blob: 8cfa9cb74c8679a329ff14293f0e1fd4b2927323 [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
David Howells760285e2012-10-02 18:01:07 +010038#include <drm/drmP.h>
39#include <drm/mga_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "mga_drv.h"
41
42#define MGA_DEFAULT_USEC_TIMEOUT 10000
43#define MGA_FREELIST_DEBUG 0
44
Dave Airlie7ccf8002005-11-11 23:11:34 +110045#define MINIMAL_CLEANUP 0
46#define FULL_CLEANUP 1
Dave Airlieeddca552007-07-11 16:09:54 +100047static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* ================================================================
50 * Engine control
51 */
52
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +020053int mga_do_wait_for_idle(drm_mga_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 u32 status = 0;
56 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100057 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Dave Airlieb5e89ed2005-09-25 14:28:13 +100059 for (i = 0; i < dev_priv->usec_timeout; i++) {
60 status = MGA_READ(MGA_STATUS) & MGA_ENGINE_IDLE_MASK;
61 if (status == MGA_ENDPRDMASTS) {
62 MGA_WRITE8(MGA_CRTC_INDEX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return 0;
64 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67
68#if MGA_DMA_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +100069 DRM_ERROR("failed!\n");
70 DRM_INFO(" status=0x%08x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#endif
Eric Anholt20caafa2007-08-25 19:22:43 +100072 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +020075static int mga_do_dma_reset(drm_mga_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
78 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
79
Dave Airlieb5e89ed2005-09-25 14:28:13 +100080 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 /* The primary DMA stream should look like new right about now.
83 */
84 primary->tail = 0;
85 primary->space = primary->size;
86 primary->last_flush = 0;
87
88 sarea_priv->last_wrap = 0;
89
90 /* FIXME: Reset counters, buffer ages etc...
91 */
92
93 /* FIXME: What else do we need to reinitialize? WARP stuff?
94 */
95
96 return 0;
97}
98
99/* ================================================================
100 * Primary DMA stream
101 */
102
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200103void mga_do_dma_flush(drm_mga_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
106 u32 head, tail;
107 u32 status = 0;
108 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000109 DMA_LOCALS;
110 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000112 /* We need to wait so that we can do an safe flush */
113 for (i = 0; i < dev_priv->usec_timeout; i++) {
114 status = MGA_READ(MGA_STATUS) & MGA_ENGINE_IDLE_MASK;
115 if (status == MGA_ENDPRDMASTS)
116 break;
117 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000120 if (primary->tail == primary->last_flush) {
121 DRM_DEBUG(" bailing out...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return;
123 }
124
125 tail = primary->tail + dev_priv->primary->offset;
126
127 /* We need to pad the stream between flushes, as the card
128 * actually (partially?) reads the first of these commands.
129 * See page 4-16 in the G400 manual, middle of the page or so.
130 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 BEGIN_DMA(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000133 DMA_BLOCK(MGA_DMAPAD, 0x00000000,
134 MGA_DMAPAD, 0x00000000,
135 MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 ADVANCE_DMA();
138
139 primary->last_flush = primary->tail;
140
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141 head = MGA_READ(MGA_PRIMADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200143 if (head <= tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 primary->space = primary->size - primary->tail;
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200145 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 primary->space = head - tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100148 DRM_DEBUG(" head = 0x%06lx\n", (unsigned long)(head - dev_priv->primary->offset));
149 DRM_DEBUG(" tail = 0x%06lx\n", (unsigned long)(tail - dev_priv->primary->offset));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000150 DRM_DEBUG(" space = 0x%06x\n", primary->space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 mga_flush_write_combine();
Dave Airlie6795c982005-07-10 18:20:09 +1000153 MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000155 DRM_DEBUG("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200158void mga_do_dma_wrap_start(drm_mga_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
161 u32 head, tail;
162 DMA_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000163 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 BEGIN_DMA_WRAP();
166
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000167 DMA_BLOCK(MGA_DMAPAD, 0x00000000,
168 MGA_DMAPAD, 0x00000000,
169 MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 ADVANCE_DMA();
172
173 tail = primary->tail + dev_priv->primary->offset;
174
175 primary->tail = 0;
176 primary->last_flush = 0;
177 primary->last_wrap++;
178
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000179 head = MGA_READ(MGA_PRIMADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200181 if (head == dev_priv->primary->offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 primary->space = primary->size;
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200183 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 primary->space = head - dev_priv->primary->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100186 DRM_DEBUG(" head = 0x%06lx\n", (unsigned long)(head - dev_priv->primary->offset));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000187 DRM_DEBUG(" tail = 0x%06x\n", primary->tail);
188 DRM_DEBUG(" wrap = %d\n", primary->last_wrap);
189 DRM_DEBUG(" space = 0x%06x\n", primary->space);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 mga_flush_write_combine();
Dave Airlie6795c982005-07-10 18:20:09 +1000192 MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194 set_bit(0, &primary->wrapped);
195 DRM_DEBUG("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200198void mga_do_dma_wrap_end(drm_mga_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
201 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
202 u32 head = dev_priv->primary->offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000203 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 sarea_priv->last_wrap++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206 DRM_DEBUG(" wrap = %d\n", sarea_priv->last_wrap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 mga_flush_write_combine();
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209 MGA_WRITE(MGA_PRIMADDRESS, head | MGA_DMA_GENERAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 clear_bit(0, &primary->wrapped);
212 DRM_DEBUG("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/* ================================================================
216 * Freelist management
217 */
218
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200219#define MGA_BUFFER_USED (~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define MGA_BUFFER_FREE 0
221
222#if MGA_FREELIST_DEBUG
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200223static void mga_freelist_print(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 drm_mga_private_t *dev_priv = dev->dev_private;
226 drm_mga_freelist_t *entry;
227
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000228 DRM_INFO("\n");
229 DRM_INFO("current dispatch: last=0x%x done=0x%x\n",
230 dev_priv->sarea_priv->last_dispatch,
231 (unsigned int)(MGA_READ(MGA_PRIMADDRESS) -
232 dev_priv->primary->offset));
233 DRM_INFO("current freelist:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000235 for (entry = dev_priv->head->next; entry; entry = entry->next) {
236 DRM_INFO(" %p idx=%2d age=0x%x 0x%06lx\n",
237 entry, entry->buf->idx, entry->age.head,
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100238 (unsigned long)(entry->age.head - dev_priv->primary->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000240 DRM_INFO("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242#endif
243
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200244static int mga_freelist_init(struct drm_device *dev, drm_mga_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Dave Airliecdd55a22007-07-11 16:32:08 +1000246 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +1000247 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 drm_mga_buf_priv_t *buf_priv;
249 drm_mga_freelist_t *entry;
250 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000251 DRM_DEBUG("count=%d\n", dma->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Eric Anholt9a298b22009-03-24 12:23:04 -0700253 dev_priv->head = kzalloc(sizeof(drm_mga_freelist_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000254 if (dev_priv->head == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000255 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000257 SET_AGE(&dev_priv->head->age, MGA_BUFFER_USED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000259 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 buf_priv = buf->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Eric Anholt9a298b22009-03-24 12:23:04 -0700263 entry = kzalloc(sizeof(drm_mga_freelist_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000264 if (entry == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000265 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 entry->next = dev_priv->head->next;
268 entry->prev = dev_priv->head;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000269 SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 entry->buf = buf;
271
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272 if (dev_priv->head->next != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 dev_priv->head->next->prev = entry;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274 if (entry->next == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 dev_priv->tail = entry;
276
277 buf_priv->list_entry = entry;
278 buf_priv->discard = 0;
279 buf_priv->dispatched = 0;
280
281 dev_priv->head->next = entry;
282 }
283
284 return 0;
285}
286
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200287static void mga_freelist_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 drm_mga_private_t *dev_priv = dev->dev_private;
290 drm_mga_freelist_t *entry;
291 drm_mga_freelist_t *next;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000292 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 entry = dev_priv->head;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000295 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 next = entry->next;
Eric Anholt9a298b22009-03-24 12:23:04 -0700297 kfree(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 entry = next;
299 }
300
301 dev_priv->head = dev_priv->tail = NULL;
302}
303
304#if 0
305/* FIXME: Still needed?
306 */
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200307static void mga_freelist_reset(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Dave Airliecdd55a22007-07-11 16:32:08 +1000309 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +1000310 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 drm_mga_buf_priv_t *buf_priv;
312 int i;
313
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000314 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 buf = dma->buflist[i];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000316 buf_priv = buf->dev_private;
317 SET_AGE(&buf_priv->list_entry->age, MGA_BUFFER_FREE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319}
320#endif
321
Dave Airlie056219e2007-07-11 16:17:42 +1000322static struct drm_buf *mga_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 drm_mga_private_t *dev_priv = dev->dev_private;
325 drm_mga_freelist_t *next;
326 drm_mga_freelist_t *prev;
327 drm_mga_freelist_t *tail = dev_priv->tail;
328 u32 head, wrap;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000329 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000331 head = MGA_READ(MGA_PRIMADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 wrap = dev_priv->sarea_priv->last_wrap;
333
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000334 DRM_DEBUG(" tail=0x%06lx %d\n",
335 tail->age.head ?
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100336 (unsigned long)(tail->age.head - dev_priv->primary->offset) : 0,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337 tail->age.wrap);
338 DRM_DEBUG(" head=0x%06lx %d\n",
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100339 (unsigned long)(head - dev_priv->primary->offset), wrap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 if (TEST_AGE(&tail->age, head, wrap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 prev = dev_priv->tail->prev;
343 next = dev_priv->tail;
344 prev->next = NULL;
345 next->prev = next->next = NULL;
346 dev_priv->tail = prev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347 SET_AGE(&next->age, MGA_BUFFER_USED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return next->buf;
349 }
350
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000351 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return NULL;
353}
354
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200355int mga_freelist_put(struct drm_device *dev, struct drm_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 drm_mga_private_t *dev_priv = dev->dev_private;
358 drm_mga_buf_priv_t *buf_priv = buf->dev_private;
359 drm_mga_freelist_t *head, *entry, *prev;
360
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000361 DRM_DEBUG("age=0x%06lx wrap=%d\n",
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100362 (unsigned long)(buf_priv->list_entry->age.head -
363 dev_priv->primary->offset),
364 buf_priv->list_entry->age.wrap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 entry = buf_priv->list_entry;
367 head = dev_priv->head;
368
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000369 if (buf_priv->list_entry->age.head == MGA_BUFFER_USED) {
370 SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 prev = dev_priv->tail;
372 prev->next = entry;
373 entry->prev = prev;
374 entry->next = NULL;
375 } else {
376 prev = head->next;
377 head->next = entry;
378 prev->prev = entry;
379 entry->prev = head;
380 entry->next = prev;
381 }
382
383 return 0;
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/* ================================================================
387 * DMA initialization, cleanup
388 */
389
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200390int mga_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie6795c982005-07-10 18:20:09 +1000391{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000392 drm_mga_private_t *dev_priv;
Keith Packard52440212008-11-18 09:30:25 -0800393 int ret;
Dave Airlie6795c982005-07-10 18:20:09 +1000394
Eric Anholt9a298b22009-03-24 12:23:04 -0700395 dev_priv = kzalloc(sizeof(drm_mga_private_t), GFP_KERNEL);
Dave Airlie6795c982005-07-10 18:20:09 +1000396 if (!dev_priv)
Eric Anholt20caafa2007-08-25 19:22:43 +1000397 return -ENOMEM;
Dave Airlie6795c982005-07-10 18:20:09 +1000398
399 dev->dev_private = (void *)dev_priv;
Dave Airlie6795c982005-07-10 18:20:09 +1000400
401 dev_priv->usec_timeout = MGA_DEFAULT_USEC_TIMEOUT;
402 dev_priv->chipset = flags;
403
Dave Airlie466e69b2011-12-19 11:15:29 +0000404 pci_set_master(dev->pdev);
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
Keith Packard52440212008-11-18 09:30:25 -0800409 ret = drm_vblank_init(dev, 1);
410
411 if (ret) {
412 (void) mga_driver_unload(dev);
413 return ret;
414 }
415
Dave Airlie6795c982005-07-10 18:20:09 +1000416 return 0;
417}
418
Dave Airlie908f9c42005-09-05 21:51:30 +1000419#if __OS_HAS_AGP
Dave Airlie6795c982005-07-10 18:20:09 +1000420/**
421 * Bootstrap the driver for AGP DMA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 *
Dave Airlie6795c982005-07-10 18:20:09 +1000423 * \todo
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300424 * Investigate whether there is any benefit to storing the WARP microcode in
Dave Airlie6795c982005-07-10 18:20:09 +1000425 * AGP memory. If not, the microcode may as well always be put in PCI
426 * memory.
427 *
428 * \todo
429 * This routine needs to set dma_bs->agp_mode to the mode actually configured
430 * in the hardware. Looking just at the Linux AGP driver code, I don't see
431 * an easy way to determine this.
432 *
433 * \sa mga_do_dma_bootstrap, mga_do_pci_dma_bootstrap
434 */
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200435static int mga_do_agp_dma_bootstrap(struct drm_device *dev,
436 drm_mga_dma_bootstrap_t *dma_bs)
Dave Airlie6795c982005-07-10 18:20:09 +1000437{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000438 drm_mga_private_t *const dev_priv =
439 (drm_mga_private_t *) dev->dev_private;
Ben Hutchingsece2be72009-08-23 18:34:25 +0100440 unsigned int warp_size = MGA_WARP_UCODE_SIZE;
Dave Airlie6795c982005-07-10 18:20:09 +1000441 int err;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000442 unsigned offset;
Dave Airlie6795c982005-07-10 18:20:09 +1000443 const unsigned secondary_size = dma_bs->secondary_bin_count
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000444 * dma_bs->secondary_bin_size;
Dave Airlie6795c982005-07-10 18:20:09 +1000445 const unsigned agp_size = (dma_bs->agp_size << 20);
Dave Airlieeddca552007-07-11 16:09:54 +1000446 struct drm_buf_desc req;
447 struct drm_agp_mode mode;
448 struct drm_agp_info info;
449 struct drm_agp_buffer agp_req;
450 struct drm_agp_binding bind_req;
Dave Airlie6795c982005-07-10 18:20:09 +1000451
Dave Airlie6795c982005-07-10 18:20:09 +1000452 /* Acquire AGP. */
453 err = drm_agp_acquire(dev);
454 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100455 DRM_ERROR("Unable to acquire AGP: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000456 return err;
457 }
458
459 err = drm_agp_info(dev, &info);
460 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100461 DRM_ERROR("Unable to get AGP info: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000462 return err;
463 }
464
465 mode.mode = (info.mode & ~0x07) | dma_bs->agp_mode;
466 err = drm_agp_enable(dev, mode);
467 if (err) {
468 DRM_ERROR("Unable to enable AGP (mode = 0x%lx)\n", mode.mode);
469 return err;
470 }
471
Dave Airlie6795c982005-07-10 18:20:09 +1000472 /* In addition to the usual AGP mode configuration, the G200 AGP cards
473 * need to have the AGP mode "manually" set.
474 */
475
476 if (dev_priv->chipset == MGA_CARD_TYPE_G200) {
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200477 if (mode.mode & 0x02)
Dave Airlie6795c982005-07-10 18:20:09 +1000478 MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_ENABLE);
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200479 else
Dave Airlie6795c982005-07-10 18:20:09 +1000480 MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_DISABLE);
Dave Airlie6795c982005-07-10 18:20:09 +1000481 }
482
Dave Airlie6795c982005-07-10 18:20:09 +1000483 /* Allocate and bind AGP memory. */
Dave Airlie7ccf8002005-11-11 23:11:34 +1100484 agp_req.size = agp_size;
485 agp_req.type = 0;
486 err = drm_agp_alloc(dev, &agp_req);
487 if (err) {
488 dev_priv->agp_size = 0;
Dave Airlie6795c982005-07-10 18:20:09 +1000489 DRM_ERROR("Unable to allocate %uMB AGP memory\n",
490 dma_bs->agp_size);
Dave Airlie7ccf8002005-11-11 23:11:34 +1100491 return err;
Dave Airlie6795c982005-07-10 18:20:09 +1000492 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000493
Dave Airlie7ccf8002005-11-11 23:11:34 +1100494 dev_priv->agp_size = agp_size;
495 dev_priv->agp_handle = agp_req.handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000496
Dave Airlie7ccf8002005-11-11 23:11:34 +1100497 bind_req.handle = agp_req.handle;
498 bind_req.offset = 0;
499 err = drm_agp_bind(dev, &bind_req);
Dave Airlie6795c982005-07-10 18:20:09 +1000500 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100501 DRM_ERROR("Unable to bind AGP memory: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000502 return err;
503 }
504
David Herrmann9fc5cde2014-08-29 12:12:28 +0200505 /* Make drm_legacy_addbufs happy by not trying to create a mapping for
506 * less than a page.
Dave Airlie11909d62005-10-19 21:23:51 -0700507 */
508 if (warp_size < PAGE_SIZE)
509 warp_size = PAGE_SIZE;
510
Dave Airlie6795c982005-07-10 18:20:09 +1000511 offset = 0;
David Herrmann9fc5cde2014-08-29 12:12:28 +0200512 err = drm_legacy_addmap(dev, offset, warp_size,
513 _DRM_AGP, _DRM_READ_ONLY, &dev_priv->warp);
Dave Airlie6795c982005-07-10 18:20:09 +1000514 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100515 DRM_ERROR("Unable to map WARP microcode: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000516 return err;
517 }
518
519 offset += warp_size;
David Herrmann9fc5cde2014-08-29 12:12:28 +0200520 err = drm_legacy_addmap(dev, offset, dma_bs->primary_size,
521 _DRM_AGP, _DRM_READ_ONLY, &dev_priv->primary);
Dave Airlie6795c982005-07-10 18:20:09 +1000522 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100523 DRM_ERROR("Unable to map primary DMA region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000524 return err;
525 }
526
527 offset += dma_bs->primary_size;
David Herrmann9fc5cde2014-08-29 12:12:28 +0200528 err = drm_legacy_addmap(dev, offset, secondary_size,
529 _DRM_AGP, 0, &dev->agp_buffer_map);
Dave Airlie6795c982005-07-10 18:20:09 +1000530 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100531 DRM_ERROR("Unable to map secondary DMA region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000532 return err;
533 }
534
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000535 (void)memset(&req, 0, sizeof(req));
Dave Airlie6795c982005-07-10 18:20:09 +1000536 req.count = dma_bs->secondary_bin_count;
537 req.size = dma_bs->secondary_bin_size;
538 req.flags = _DRM_AGP_BUFFER;
539 req.agp_start = offset;
540
David Herrmann9fc5cde2014-08-29 12:12:28 +0200541 err = drm_legacy_addbufs_agp(dev, &req);
Dave Airlie6795c982005-07-10 18:20:09 +1000542 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100543 DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000544 return err;
545 }
546
Dave Airlie7ccf8002005-11-11 23:11:34 +1100547 {
Dave Airlie55910512007-07-11 16:53:40 +1000548 struct drm_map_list *_entry;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100549 unsigned long agp_token = 0;
Dave Airliebc5f4522007-11-05 12:50:58 +1000550
Dave Airliebd1b3312007-05-26 05:01:51 +1000551 list_for_each_entry(_entry, &dev->maplist, head) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100552 if (_entry->map == dev->agp_buffer_map)
553 agp_token = _entry->user_token;
554 }
555 if (!agp_token)
556 return -EFAULT;
557
558 dev->agp_buffer_token = agp_token;
559 }
560
Dave Airlie6795c982005-07-10 18:20:09 +1000561 offset += secondary_size;
David Herrmann9fc5cde2014-08-29 12:12:28 +0200562 err = drm_legacy_addmap(dev, offset, agp_size - offset,
563 _DRM_AGP, 0, &dev_priv->agp_textures);
Dave Airlie6795c982005-07-10 18:20:09 +1000564 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100565 DRM_ERROR("Unable to map AGP texture region %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000566 return err;
567 }
568
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200569 drm_legacy_ioremap(dev_priv->warp, dev);
570 drm_legacy_ioremap(dev_priv->primary, dev);
571 drm_legacy_ioremap(dev->agp_buffer_map, dev);
Dave Airlie6795c982005-07-10 18:20:09 +1000572
573 if (!dev_priv->warp->handle ||
574 !dev_priv->primary->handle || !dev->agp_buffer_map->handle) {
575 DRM_ERROR("failed to ioremap agp regions! (%p, %p, %p)\n",
576 dev_priv->warp->handle, dev_priv->primary->handle,
577 dev->agp_buffer_map->handle);
Eric Anholt20caafa2007-08-25 19:22:43 +1000578 return -ENOMEM;
Dave Airlie6795c982005-07-10 18:20:09 +1000579 }
580
581 dev_priv->dma_access = MGA_PAGPXFER;
582 dev_priv->wagp_enable = MGA_WAGP_ENABLE;
583
584 DRM_INFO("Initialized card for AGP DMA.\n");
585 return 0;
586}
Dave Airlie908f9c42005-09-05 21:51:30 +1000587#else
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200588static int mga_do_agp_dma_bootstrap(struct drm_device *dev,
589 drm_mga_dma_bootstrap_t *dma_bs)
Dave Airlie908f9c42005-09-05 21:51:30 +1000590{
591 return -EINVAL;
592}
593#endif
Dave Airlie6795c982005-07-10 18:20:09 +1000594
595/**
596 * Bootstrap the driver for PCI DMA.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597 *
Dave Airlie6795c982005-07-10 18:20:09 +1000598 * \todo
599 * The algorithm for decreasing the size of the primary DMA buffer could be
600 * better. The size should be rounded up to the nearest page size, then
601 * decrease the request size by a single page each pass through the loop.
602 *
603 * \todo
604 * Determine whether the maximum address passed to drm_pci_alloc is correct.
David Herrmann9fc5cde2014-08-29 12:12:28 +0200605 * The same goes for drm_legacy_addbufs_pci.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606 *
Dave Airlie6795c982005-07-10 18:20:09 +1000607 * \sa mga_do_dma_bootstrap, mga_do_agp_dma_bootstrap
608 */
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200609static int mga_do_pci_dma_bootstrap(struct drm_device *dev,
610 drm_mga_dma_bootstrap_t *dma_bs)
Dave Airlie6795c982005-07-10 18:20:09 +1000611{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000612 drm_mga_private_t *const dev_priv =
613 (drm_mga_private_t *) dev->dev_private;
Ben Hutchingsece2be72009-08-23 18:34:25 +0100614 unsigned int warp_size = MGA_WARP_UCODE_SIZE;
Dave Airlie6795c982005-07-10 18:20:09 +1000615 unsigned int primary_size;
616 unsigned int bin_count;
617 int err;
Dave Airlieeddca552007-07-11 16:09:54 +1000618 struct drm_buf_desc req;
Dave Airlie6795c982005-07-10 18:20:09 +1000619
Dave Airlie6795c982005-07-10 18:20:09 +1000620 if (dev->dma == NULL) {
621 DRM_ERROR("dev->dma is NULL\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000622 return -EFAULT;
Dave Airlie6795c982005-07-10 18:20:09 +1000623 }
624
David Herrmann9fc5cde2014-08-29 12:12:28 +0200625 /* Make drm_legacy_addbufs happy by not trying to create a mapping for
626 * less than a page.
Dave Airlie11909d62005-10-19 21:23:51 -0700627 */
628 if (warp_size < PAGE_SIZE)
629 warp_size = PAGE_SIZE;
630
Dave Airlie6795c982005-07-10 18:20:09 +1000631 /* The proper alignment is 0x100 for this mapping */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200632 err = drm_legacy_addmap(dev, 0, warp_size, _DRM_CONSISTENT,
633 _DRM_READ_ONLY, &dev_priv->warp);
Dave Airlie6795c982005-07-10 18:20:09 +1000634 if (err != 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100635 DRM_ERROR("Unable to create mapping for WARP microcode: %d\n",
636 err);
Dave Airlie6795c982005-07-10 18:20:09 +1000637 return err;
638 }
639
640 /* Other than the bottom two bits being used to encode other
641 * information, there don't appear to be any restrictions on the
642 * alignment of the primary or secondary DMA buffers.
643 */
644
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000645 for (primary_size = dma_bs->primary_size; primary_size != 0;
646 primary_size >>= 1) {
Dave Airlie6795c982005-07-10 18:20:09 +1000647 /* The proper alignment for this mapping is 0x04 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200648 err = drm_legacy_addmap(dev, 0, primary_size, _DRM_CONSISTENT,
649 _DRM_READ_ONLY, &dev_priv->primary);
Dave Airlie6795c982005-07-10 18:20:09 +1000650 if (!err)
651 break;
652 }
653
654 if (err != 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100655 DRM_ERROR("Unable to allocate primary DMA region: %d\n", err);
Eric Anholt20caafa2007-08-25 19:22:43 +1000656 return -ENOMEM;
Dave Airlie6795c982005-07-10 18:20:09 +1000657 }
658
659 if (dev_priv->primary->size != dma_bs->primary_size) {
660 DRM_INFO("Primary DMA buffer size reduced from %u to %u.\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000661 dma_bs->primary_size,
662 (unsigned)dev_priv->primary->size);
Dave Airlie6795c982005-07-10 18:20:09 +1000663 dma_bs->primary_size = dev_priv->primary->size;
664 }
665
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000666 for (bin_count = dma_bs->secondary_bin_count; bin_count > 0;
667 bin_count--) {
668 (void)memset(&req, 0, sizeof(req));
Dave Airlie6795c982005-07-10 18:20:09 +1000669 req.count = bin_count;
670 req.size = dma_bs->secondary_bin_size;
671
David Herrmann9fc5cde2014-08-29 12:12:28 +0200672 err = drm_legacy_addbufs_pci(dev, &req);
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200673 if (!err)
Dave Airlie6795c982005-07-10 18:20:09 +1000674 break;
Dave Airlie6795c982005-07-10 18:20:09 +1000675 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000676
Dave Airlie6795c982005-07-10 18:20:09 +1000677 if (bin_count == 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100678 DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000679 return err;
680 }
681
682 if (bin_count != dma_bs->secondary_bin_count) {
683 DRM_INFO("Secondary PCI DMA buffer bin count reduced from %u "
684 "to %u.\n", dma_bs->secondary_bin_count, bin_count);
685
686 dma_bs->secondary_bin_count = bin_count;
687 }
688
689 dev_priv->dma_access = 0;
690 dev_priv->wagp_enable = 0;
691
692 dma_bs->agp_mode = 0;
693
694 DRM_INFO("Initialized card for PCI DMA.\n");
695 return 0;
696}
697
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200698static int mga_do_dma_bootstrap(struct drm_device *dev,
699 drm_mga_dma_bootstrap_t *dma_bs)
Dave Airlie6795c982005-07-10 18:20:09 +1000700{
Dave Airlie8410ea32010-12-15 03:16:38 +1000701 const int is_agp = (dma_bs->agp_mode != 0) && drm_pci_device_is_agp(dev);
Dave Airlie6795c982005-07-10 18:20:09 +1000702 int err;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000703 drm_mga_private_t *const dev_priv =
704 (drm_mga_private_t *) dev->dev_private;
Dave Airlie6795c982005-07-10 18:20:09 +1000705
706 dev_priv->used_new_dma_init = 1;
707
708 /* The first steps are the same for both PCI and AGP based DMA. Map
709 * the cards MMIO registers and map a status page.
710 */
David Herrmann9fc5cde2014-08-29 12:12:28 +0200711 err = drm_legacy_addmap(dev, dev_priv->mmio_base, dev_priv->mmio_size,
712 _DRM_REGISTERS, _DRM_READ_ONLY,
713 &dev_priv->mmio);
Dave Airlie6795c982005-07-10 18:20:09 +1000714 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100715 DRM_ERROR("Unable to map MMIO region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000716 return err;
717 }
718
David Herrmann9fc5cde2014-08-29 12:12:28 +0200719 err = drm_legacy_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
720 _DRM_READ_ONLY | _DRM_LOCKED | _DRM_KERNEL,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 &dev_priv->status);
Dave Airlie6795c982005-07-10 18:20:09 +1000722 if (err) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100723 DRM_ERROR("Unable to map status region: %d\n", err);
Dave Airlie6795c982005-07-10 18:20:09 +1000724 return err;
725 }
726
Dave Airlie6795c982005-07-10 18:20:09 +1000727 /* The DMA initialization procedure is slightly different for PCI and
728 * AGP cards. AGP cards just allocate a large block of AGP memory and
729 * carve off portions of it for internal uses. The remaining memory
730 * is returned to user-mode to be used for AGP textures.
731 */
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200732 if (is_agp)
Dave Airlie6795c982005-07-10 18:20:09 +1000733 err = mga_do_agp_dma_bootstrap(dev, dma_bs);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000734
Dave Airlie6795c982005-07-10 18:20:09 +1000735 /* If we attempted to initialize the card for AGP DMA but failed,
736 * clean-up any mess that may have been created.
737 */
738
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200739 if (err)
Dave Airlie7ccf8002005-11-11 23:11:34 +1100740 mga_do_cleanup_dma(dev, MINIMAL_CLEANUP);
Dave Airlie6795c982005-07-10 18:20:09 +1000741
Dave Airlie6795c982005-07-10 18:20:09 +1000742 /* Not only do we want to try and initialized PCI cards for PCI DMA,
743 * but we also try to initialized AGP cards that could not be
744 * initialized for AGP DMA. This covers the case where we have an AGP
745 * card in a system with an unsupported AGP chipset. In that case the
746 * card will be detected as AGP, but we won't be able to allocate any
747 * AGP memory, etc.
748 */
749
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200750 if (!is_agp || err)
Dave Airlie6795c982005-07-10 18:20:09 +1000751 err = mga_do_pci_dma_bootstrap(dev, dma_bs);
Dave Airlie6795c982005-07-10 18:20:09 +1000752
Dave Airlie6795c982005-07-10 18:20:09 +1000753 return err;
754}
755
Eric Anholtc153f452007-09-03 12:06:45 +1000756int mga_dma_bootstrap(struct drm_device *dev, void *data,
757 struct drm_file *file_priv)
Dave Airlie6795c982005-07-10 18:20:09 +1000758{
Eric Anholtc153f452007-09-03 12:06:45 +1000759 drm_mga_dma_bootstrap_t *bootstrap = data;
Dave Airlie6795c982005-07-10 18:20:09 +1000760 int err;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100761 static const int modes[] = { 0, 1, 2, 2, 4, 4, 4, 4 };
762 const drm_mga_private_t *const dev_priv =
763 (drm_mga_private_t *) dev->dev_private;
Dave Airlie6795c982005-07-10 18:20:09 +1000764
Eric Anholtc153f452007-09-03 12:06:45 +1000765 err = mga_do_dma_bootstrap(dev, bootstrap);
Dave Airlie7ccf8002005-11-11 23:11:34 +1100766 if (err) {
767 mga_do_cleanup_dma(dev, FULL_CLEANUP);
768 return err;
Dave Airlie6795c982005-07-10 18:20:09 +1000769 }
770
Dave Airlie7ccf8002005-11-11 23:11:34 +1100771 if (dev_priv->agp_textures != NULL) {
Eric Anholtc153f452007-09-03 12:06:45 +1000772 bootstrap->texture_handle = dev_priv->agp_textures->offset;
773 bootstrap->texture_size = dev_priv->agp_textures->size;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100774 } else {
Eric Anholtc153f452007-09-03 12:06:45 +1000775 bootstrap->texture_handle = 0;
776 bootstrap->texture_size = 0;
Dave Airlie7ccf8002005-11-11 23:11:34 +1100777 }
778
Eric Anholtc153f452007-09-03 12:06:45 +1000779 bootstrap->agp_mode = modes[bootstrap->agp_mode & 0x07];
Dave Airlie7ccf8002005-11-11 23:11:34 +1100780
Dave Airlie6795c982005-07-10 18:20:09 +1000781 return err;
782}
783
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200784static int mga_do_init_dma(struct drm_device *dev, drm_mga_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 drm_mga_private_t *dev_priv;
787 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000788 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Dave Airlie6795c982005-07-10 18:20:09 +1000790 dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200792 if (init->sgram)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_BLK;
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200794 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_RSTR;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000796 dev_priv->maccess = init->maccess;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 dev_priv->fb_cpp = init->fb_cpp;
799 dev_priv->front_offset = init->front_offset;
800 dev_priv->front_pitch = init->front_pitch;
801 dev_priv->back_offset = init->back_offset;
802 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000804 dev_priv->depth_cpp = init->depth_cpp;
805 dev_priv->depth_offset = init->depth_offset;
806 dev_priv->depth_pitch = init->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /* FIXME: Need to support AGP textures...
809 */
810 dev_priv->texture_offset = init->texture_offset[0];
811 dev_priv->texture_size = init->texture_size[0];
812
David Herrmann9fc5cde2014-08-29 12:12:28 +0200813 dev_priv->sarea = drm_legacy_getsarea(dev);
Dave Airlie6795c982005-07-10 18:20:09 +1000814 if (!dev_priv->sarea) {
815 DRM_ERROR("failed to find sarea!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000816 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000819 if (!dev_priv->used_new_dma_init) {
Dave Airlie11909d62005-10-19 21:23:51 -0700820
821 dev_priv->dma_access = MGA_PAGPXFER;
822 dev_priv->wagp_enable = MGA_WAGP_ENABLE;
823
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200824 dev_priv->status = drm_legacy_findmap(dev, init->status_offset);
Dave Airlie6795c982005-07-10 18:20:09 +1000825 if (!dev_priv->status) {
826 DRM_ERROR("failed to find status page!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000827 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000828 }
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200829 dev_priv->mmio = drm_legacy_findmap(dev, init->mmio_offset);
Dave Airlie6795c982005-07-10 18:20:09 +1000830 if (!dev_priv->mmio) {
831 DRM_ERROR("failed to find mmio region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000832 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000833 }
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200834 dev_priv->warp = drm_legacy_findmap(dev, init->warp_offset);
Dave Airlie6795c982005-07-10 18:20:09 +1000835 if (!dev_priv->warp) {
836 DRM_ERROR("failed to find warp microcode region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000837 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000838 }
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200839 dev_priv->primary = drm_legacy_findmap(dev, init->primary_offset);
Dave Airlie6795c982005-07-10 18:20:09 +1000840 if (!dev_priv->primary) {
841 DRM_ERROR("failed to find primary dma region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000842 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000843 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000844 dev->agp_buffer_token = init->buffers_offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845 dev->agp_buffer_map =
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200846 drm_legacy_findmap(dev, init->buffers_offset);
Dave Airlie6795c982005-07-10 18:20:09 +1000847 if (!dev->agp_buffer_map) {
848 DRM_ERROR("failed to find dma buffer region!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000849 return -EINVAL;
Dave Airlie6795c982005-07-10 18:20:09 +1000850 }
851
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200852 drm_legacy_ioremap(dev_priv->warp, dev);
853 drm_legacy_ioremap(dev_priv->primary, dev);
854 drm_legacy_ioremap(dev->agp_buffer_map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 dev_priv->sarea_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000858 (drm_mga_sarea_t *) ((u8 *) dev_priv->sarea->handle +
859 init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Dave Airlie6795c982005-07-10 18:20:09 +1000861 if (!dev_priv->warp->handle ||
862 !dev_priv->primary->handle ||
863 ((dev_priv->dma_access != 0) &&
864 ((dev->agp_buffer_map == NULL) ||
865 (dev->agp_buffer_map->handle == NULL)))) {
866 DRM_ERROR("failed to ioremap agp regions!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000867 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
Dave Airlie6795c982005-07-10 18:20:09 +1000870 ret = mga_warp_install_microcode(dev_priv);
871 if (ret < 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100872 DRM_ERROR("failed to install WARP ucode!: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return ret;
874 }
875
Dave Airlie6795c982005-07-10 18:20:09 +1000876 ret = mga_warp_init(dev_priv);
877 if (ret < 0) {
Dave Airlie7ccf8002005-11-11 23:11:34 +1100878 DRM_ERROR("failed to init WARP engine!: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return ret;
880 }
881
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000882 dev_priv->prim.status = (u32 *) dev_priv->status->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000884 mga_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 /* Init the primary DMA registers.
887 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 MGA_WRITE(MGA_PRIMADDRESS, dev_priv->primary->offset | MGA_DMA_GENERAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#if 0
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 MGA_WRITE(MGA_PRIMPTR, virt_to_bus((void *)dev_priv->prim.status) | MGA_PRIMPTREN0 | /* Soft trap, SECEND, SETUPEND */
891 MGA_PRIMPTREN1); /* DWGSYNC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892#endif
893
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000894 dev_priv->prim.start = (u8 *) dev_priv->primary->handle;
895 dev_priv->prim.end = ((u8 *) dev_priv->primary->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 + dev_priv->primary->size);
897 dev_priv->prim.size = dev_priv->primary->size;
898
899 dev_priv->prim.tail = 0;
900 dev_priv->prim.space = dev_priv->prim.size;
901 dev_priv->prim.wrapped = 0;
902
903 dev_priv->prim.last_flush = 0;
904 dev_priv->prim.last_wrap = 0;
905
906 dev_priv->prim.high_mark = 256 * DMA_BLOCK_SIZE;
907
908 dev_priv->prim.status[0] = dev_priv->primary->offset;
909 dev_priv->prim.status[1] = 0;
910
911 dev_priv->sarea_priv->last_wrap = 0;
912 dev_priv->sarea_priv->last_frame.head = 0;
913 dev_priv->sarea_priv->last_frame.wrap = 0;
914
Dave Airlie6795c982005-07-10 18:20:09 +1000915 if (mga_freelist_init(dev, dev_priv) < 0) {
916 DRM_ERROR("could not initialize freelist\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000917 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
921}
922
Dave Airlieeddca552007-07-11 16:09:54 +1000923static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Dave Airlie6795c982005-07-10 18:20:09 +1000925 int err = 0;
926 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 /* Make sure interrupts are disabled here because the uninstall ioctl
929 * may not have been called from userspace and after dev_private
930 * is freed, it's too late.
931 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000932 if (dev->irq_enabled)
933 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000935 if (dev->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 drm_mga_private_t *dev_priv = dev->dev_private;
937
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 if ((dev_priv->warp != NULL)
Dave Airlie11909d62005-10-19 21:23:51 -0700939 && (dev_priv->warp->type != _DRM_CONSISTENT))
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200940 drm_legacy_ioremapfree(dev_priv->warp, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 if ((dev_priv->primary != NULL)
Dave Airlie6795c982005-07-10 18:20:09 +1000943 && (dev_priv->primary->type != _DRM_CONSISTENT))
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200944 drm_legacy_ioremapfree(dev_priv->primary, dev);
Dave Airlie6795c982005-07-10 18:20:09 +1000945
946 if (dev->agp_buffer_map != NULL)
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200947 drm_legacy_ioremapfree(dev->agp_buffer_map, dev);
Dave Airlie6795c982005-07-10 18:20:09 +1000948
949 if (dev_priv->used_new_dma_init) {
Dave Airlie908f9c42005-09-05 21:51:30 +1000950#if __OS_HAS_AGP
Dave Airlie7ccf8002005-11-11 23:11:34 +1100951 if (dev_priv->agp_handle != 0) {
Dave Airlieeddca552007-07-11 16:09:54 +1000952 struct drm_agp_binding unbind_req;
953 struct drm_agp_buffer free_req;
Dave Airlie6795c982005-07-10 18:20:09 +1000954
Dave Airlie7ccf8002005-11-11 23:11:34 +1100955 unbind_req.handle = dev_priv->agp_handle;
956 drm_agp_unbind(dev, &unbind_req);
957
958 free_req.handle = dev_priv->agp_handle;
959 drm_agp_free(dev, &free_req);
Dave Airliebc5f4522007-11-05 12:50:58 +1000960
Dave Airlie7ccf8002005-11-11 23:11:34 +1100961 dev_priv->agp_textures = NULL;
962 dev_priv->agp_size = 0;
963 dev_priv->agp_handle = 0;
Dave Airlie6795c982005-07-10 18:20:09 +1000964 }
965
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200966 if ((dev->agp != NULL) && dev->agp->acquired)
Dave Airlie6795c982005-07-10 18:20:09 +1000967 err = drm_agp_release(dev);
Dave Airlie908f9c42005-09-05 21:51:30 +1000968#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970
Dave Airlie6795c982005-07-10 18:20:09 +1000971 dev_priv->warp = NULL;
972 dev_priv->primary = NULL;
Dave Airlie6795c982005-07-10 18:20:09 +1000973 dev_priv->sarea = NULL;
974 dev_priv->sarea_priv = NULL;
975 dev->agp_buffer_map = NULL;
976
Dave Airlie7ccf8002005-11-11 23:11:34 +1100977 if (full_cleanup) {
978 dev_priv->mmio = NULL;
979 dev_priv->status = NULL;
980 dev_priv->used_new_dma_init = 0;
981 }
982
Dave Airlie6795c982005-07-10 18:20:09 +1000983 memset(&dev_priv->prim, 0, sizeof(dev_priv->prim));
984 dev_priv->warp_pipe = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000985 memset(dev_priv->warp_pipe_phys, 0,
986 sizeof(dev_priv->warp_pipe_phys));
Dave Airlie6795c982005-07-10 18:20:09 +1000987
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +0200988 if (dev_priv->head != NULL)
Dave Airlie6795c982005-07-10 18:20:09 +1000989 mga_freelist_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
Jesper Juhla96ca102007-12-17 09:47:17 +1000992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Eric Anholtc153f452007-09-03 12:06:45 +1000995int mga_dma_init(struct drm_device *dev, void *data,
996 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Eric Anholtc153f452007-09-03 12:06:45 +1000998 drm_mga_init_t *init = data;
Dave Airlie6795c982005-07-10 18:20:09 +1000999 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Eric Anholt6c340ea2007-08-25 20:23:09 +10001001 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Eric Anholtc153f452007-09-03 12:06:45 +10001003 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 case MGA_INIT_DMA:
Eric Anholtc153f452007-09-03 12:06:45 +10001005 err = mga_do_init_dma(dev, init);
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001006 if (err)
Dave Airlie7ccf8002005-11-11 23:11:34 +11001007 (void)mga_do_cleanup_dma(dev, FULL_CLEANUP);
Dave Airlie6795c982005-07-10 18:20:09 +10001008 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 case MGA_CLEANUP_DMA:
Dave Airlie7ccf8002005-11-11 23:11:34 +11001010 return mga_do_cleanup_dma(dev, FULL_CLEANUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012
Eric Anholt20caafa2007-08-25 19:22:43 +10001013 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016/* ================================================================
1017 * Primary DMA stream management
1018 */
1019
Eric Anholtc153f452007-09-03 12:06:45 +10001020int mga_dma_flush(struct drm_device *dev, void *data,
1021 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001023 drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001024 struct drm_lock *lock = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Eric Anholt6c340ea2007-08-25 20:23:09 +10001026 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001028 DRM_DEBUG("%s%s%s\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001029 (lock->flags & _DRM_LOCK_FLUSH) ? "flush, " : "",
1030 (lock->flags & _DRM_LOCK_FLUSH_ALL) ? "flush all, " : "",
1031 (lock->flags & _DRM_LOCK_QUIESCENT) ? "idle, " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001033 WRAP_WAIT_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001035 if (lock->flags & (_DRM_LOCK_FLUSH | _DRM_LOCK_FLUSH_ALL))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036 mga_do_dma_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Eric Anholtc153f452007-09-03 12:06:45 +10001038 if (lock->flags & _DRM_LOCK_QUIESCENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039#if MGA_DMA_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 int ret = mga_do_wait_for_idle(dev_priv);
1041 if (ret < 0)
Márton Németh3e684ea2008-01-24 15:58:57 +10001042 DRM_INFO("-EBUSY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return ret;
1044#else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001045 return mga_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046#endif
1047 } else {
1048 return 0;
1049 }
1050}
1051
Eric Anholtc153f452007-09-03 12:06:45 +10001052int mga_dma_reset(struct drm_device *dev, void *data,
1053 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Eric Anholt6c340ea2007-08-25 20:23:09 +10001057 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001059 return mga_do_dma_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062/* ================================================================
1063 * DMA buffer management
1064 */
1065
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001066static int mga_dma_get_buffers(struct drm_device *dev,
1067 struct drm_file *file_priv, struct drm_dma *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Dave Airlie056219e2007-07-11 16:17:42 +10001069 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 int i;
1071
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 for (i = d->granted_count; i < d->request_count; i++) {
1073 buf = mga_freelist_get(dev);
1074 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +10001075 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Eric Anholt6c340ea2007-08-25 20:23:09 +10001077 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Daniel Vetter1d6ac182013-12-11 11:34:44 +01001079 if (copy_to_user(&d->request_indices[i],
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 &buf->idx, sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001081 return -EFAULT;
Daniel Vetter1d6ac182013-12-11 11:34:44 +01001082 if (copy_to_user(&d->request_sizes[i],
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 &buf->total, sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001084 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 d->granted_count++;
1087 }
1088 return 0;
1089}
1090
Eric Anholtc153f452007-09-03 12:06:45 +10001091int mga_dma_buffers(struct drm_device *dev, void *data,
1092 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Dave Airliecdd55a22007-07-11 16:32:08 +10001094 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001096 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int ret = 0;
1098
Eric Anholt6c340ea2007-08-25 20:23:09 +10001099 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /* Please don't send us buffers.
1102 */
Eric Anholtc153f452007-09-03 12:06:45 +10001103 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001104 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001105 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001106 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
1109 /* We'll send you buffers.
1110 */
Eric Anholtc153f452007-09-03 12:06:45 +10001111 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001113 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001114 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001117 WRAP_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Eric Anholtc153f452007-09-03 12:06:45 +10001119 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001121 if (d->request_count)
Eric Anholtc153f452007-09-03 12:06:45 +10001122 ret = mga_dma_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return ret;
1125}
1126
Dave Airlie6795c982005-07-10 18:20:09 +10001127/**
1128 * Called just before the module is unloaded.
1129 */
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001130int mga_driver_unload(struct drm_device *dev)
Dave Airlie6795c982005-07-10 18:20:09 +10001131{
Eric Anholt9a298b22009-03-24 12:23:04 -07001132 kfree(dev->dev_private);
Dave Airlie6795c982005-07-10 18:20:09 +10001133 dev->dev_private = NULL;
1134
1135 return 0;
1136}
1137
1138/**
1139 * Called when the last opener of the device is closed.
1140 */
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001141void mga_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Dave Airlie7ccf8002005-11-11 23:11:34 +11001143 mga_do_cleanup_dma(dev, FULL_CLEANUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Nicolas Kaiserf2b2cb72010-07-12 01:46:57 +02001146int mga_driver_dma_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 drm_mga_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001149 return mga_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}