mga_dma.c 29.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* mga_dma.c -- DMA support for mga g200/g400 -*- linux-c -*-
 * Created: Mon Dec 13 01:50:01 1999 by jhartmann@precisioninsight.com
 *
 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
26 27 28 29 30
 */

/**
 * \file mga_dma.c
 * DMA support for MGA G200 / G400.
D
Dave Airlie 已提交
31
 *
32 33 34 35
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 * \author Jeff Hartmann <jhartmann@valinux.com>
 * \author Keith Whitwell <keith@tungstengraphics.com>
 * \author Gareth Hughes <gareth@valinux.com>
L
Linus Torvalds 已提交
36 37 38 39
 */

#include "drmP.h"
#include "drm.h"
40
#include "drm_sarea.h"
L
Linus Torvalds 已提交
41 42 43 44 45 46
#include "mga_drm.h"
#include "mga_drv.h"

#define MGA_DEFAULT_USEC_TIMEOUT	10000
#define MGA_FREELIST_DEBUG		0

47 48
#define MINIMAL_CLEANUP 0
#define FULL_CLEANUP 1
49
static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup);
L
Linus Torvalds 已提交
50 51 52 53 54

/* ================================================================
 * Engine control
 */

D
Dave Airlie 已提交
55
int mga_do_wait_for_idle(drm_mga_private_t * dev_priv)
L
Linus Torvalds 已提交
56 57 58
{
	u32 status = 0;
	int i;
D
Dave Airlie 已提交
59
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
60

D
Dave Airlie 已提交
61 62 63 64
	for (i = 0; i < dev_priv->usec_timeout; i++) {
		status = MGA_READ(MGA_STATUS) & MGA_ENGINE_IDLE_MASK;
		if (status == MGA_ENDPRDMASTS) {
			MGA_WRITE8(MGA_CRTC_INDEX, 0);
L
Linus Torvalds 已提交
65 66
			return 0;
		}
D
Dave Airlie 已提交
67
		DRM_UDELAY(1);
L
Linus Torvalds 已提交
68 69 70
	}

#if MGA_DMA_DEBUG
D
Dave Airlie 已提交
71 72
	DRM_ERROR("failed!\n");
	DRM_INFO("   status=0x%08x\n", status);
L
Linus Torvalds 已提交
73
#endif
E
Eric Anholt 已提交
74
	return -EBUSY;
L
Linus Torvalds 已提交
75 76
}

D
Dave Airlie 已提交
77
static int mga_do_dma_reset(drm_mga_private_t * dev_priv)
L
Linus Torvalds 已提交
78 79 80 81
{
	drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
	drm_mga_primary_buffer_t *primary = &dev_priv->prim;

D
Dave Airlie 已提交
82
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

	/* The primary DMA stream should look like new right about now.
	 */
	primary->tail = 0;
	primary->space = primary->size;
	primary->last_flush = 0;

	sarea_priv->last_wrap = 0;

	/* FIXME: Reset counters, buffer ages etc...
	 */

	/* FIXME: What else do we need to reinitialize?  WARP stuff?
	 */

	return 0;
}

/* ================================================================
 * Primary DMA stream
 */

D
Dave Airlie 已提交
105
void mga_do_dma_flush(drm_mga_private_t * dev_priv)
L
Linus Torvalds 已提交
106 107 108 109 110
{
	drm_mga_primary_buffer_t *primary = &dev_priv->prim;
	u32 head, tail;
	u32 status = 0;
	int i;
D
Dave Airlie 已提交
111 112 113 114 115 116 117 118 119
	DMA_LOCALS;
	DRM_DEBUG("\n");

	/* We need to wait so that we can do an safe flush */
	for (i = 0; i < dev_priv->usec_timeout; i++) {
		status = MGA_READ(MGA_STATUS) & MGA_ENGINE_IDLE_MASK;
		if (status == MGA_ENDPRDMASTS)
			break;
		DRM_UDELAY(1);
L
Linus Torvalds 已提交
120 121
	}

D
Dave Airlie 已提交
122 123
	if (primary->tail == primary->last_flush) {
		DRM_DEBUG("   bailing out...\n");
L
Linus Torvalds 已提交
124 125 126 127 128 129 130 131 132
		return;
	}

	tail = primary->tail + dev_priv->primary->offset;

	/* We need to pad the stream between flushes, as the card
	 * actually (partially?) reads the first of these commands.
	 * See page 4-16 in the G400 manual, middle of the page or so.
	 */
D
Dave Airlie 已提交
133
	BEGIN_DMA(1);
L
Linus Torvalds 已提交
134

D
Dave Airlie 已提交
135 136 137
	DMA_BLOCK(MGA_DMAPAD, 0x00000000,
		  MGA_DMAPAD, 0x00000000,
		  MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000);
L
Linus Torvalds 已提交
138 139 140 141 142

	ADVANCE_DMA();

	primary->last_flush = primary->tail;

D
Dave Airlie 已提交
143
	head = MGA_READ(MGA_PRIMADDRESS);
L
Linus Torvalds 已提交
144

D
Dave Airlie 已提交
145
	if (head <= tail) {
L
Linus Torvalds 已提交
146 147 148 149 150
		primary->space = primary->size - primary->tail;
	} else {
		primary->space = head - tail;
	}

151 152
	DRM_DEBUG("   head = 0x%06lx\n", (unsigned long)(head - dev_priv->primary->offset));
	DRM_DEBUG("   tail = 0x%06lx\n", (unsigned long)(tail - dev_priv->primary->offset));
D
Dave Airlie 已提交
153
	DRM_DEBUG("  space = 0x%06x\n", primary->space);
L
Linus Torvalds 已提交
154 155

	mga_flush_write_combine();
156
	MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
L
Linus Torvalds 已提交
157

D
Dave Airlie 已提交
158
	DRM_DEBUG("done.\n");
L
Linus Torvalds 已提交
159 160
}

D
Dave Airlie 已提交
161
void mga_do_dma_wrap_start(drm_mga_private_t * dev_priv)
L
Linus Torvalds 已提交
162 163 164 165
{
	drm_mga_primary_buffer_t *primary = &dev_priv->prim;
	u32 head, tail;
	DMA_LOCALS;
D
Dave Airlie 已提交
166
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
167 168 169

	BEGIN_DMA_WRAP();

D
Dave Airlie 已提交
170 171 172
	DMA_BLOCK(MGA_DMAPAD, 0x00000000,
		  MGA_DMAPAD, 0x00000000,
		  MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000);
L
Linus Torvalds 已提交
173 174 175 176 177 178 179 180 181

	ADVANCE_DMA();

	tail = primary->tail + dev_priv->primary->offset;

	primary->tail = 0;
	primary->last_flush = 0;
	primary->last_wrap++;

D
Dave Airlie 已提交
182
	head = MGA_READ(MGA_PRIMADDRESS);
L
Linus Torvalds 已提交
183

D
Dave Airlie 已提交
184
	if (head == dev_priv->primary->offset) {
L
Linus Torvalds 已提交
185 186 187 188 189
		primary->space = primary->size;
	} else {
		primary->space = head - dev_priv->primary->offset;
	}

190
	DRM_DEBUG("   head = 0x%06lx\n", (unsigned long)(head - dev_priv->primary->offset));
D
Dave Airlie 已提交
191 192 193
	DRM_DEBUG("   tail = 0x%06x\n", primary->tail);
	DRM_DEBUG("   wrap = %d\n", primary->last_wrap);
	DRM_DEBUG("  space = 0x%06x\n", primary->space);
L
Linus Torvalds 已提交
194 195

	mga_flush_write_combine();
196
	MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
L
Linus Torvalds 已提交
197

D
Dave Airlie 已提交
198 199
	set_bit(0, &primary->wrapped);
	DRM_DEBUG("done.\n");
L
Linus Torvalds 已提交
200 201
}

D
Dave Airlie 已提交
202
void mga_do_dma_wrap_end(drm_mga_private_t * dev_priv)
L
Linus Torvalds 已提交
203 204 205 206
{
	drm_mga_primary_buffer_t *primary = &dev_priv->prim;
	drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
	u32 head = dev_priv->primary->offset;
D
Dave Airlie 已提交
207
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
208 209

	sarea_priv->last_wrap++;
D
Dave Airlie 已提交
210
	DRM_DEBUG("   wrap = %d\n", sarea_priv->last_wrap);
L
Linus Torvalds 已提交
211 212

	mga_flush_write_combine();
D
Dave Airlie 已提交
213
	MGA_WRITE(MGA_PRIMADDRESS, head | MGA_DMA_GENERAL);
L
Linus Torvalds 已提交
214

D
Dave Airlie 已提交
215 216
	clear_bit(0, &primary->wrapped);
	DRM_DEBUG("done.\n");
L
Linus Torvalds 已提交
217 218 219 220 221 222 223 224 225 226
}

/* ================================================================
 * Freelist management
 */

#define MGA_BUFFER_USED		~0
#define MGA_BUFFER_FREE		0

#if MGA_FREELIST_DEBUG
227
static void mga_freelist_print(struct drm_device * dev)
L
Linus Torvalds 已提交
228 229 230 231
{
	drm_mga_private_t *dev_priv = dev->dev_private;
	drm_mga_freelist_t *entry;

D
Dave Airlie 已提交
232 233 234 235 236 237 238 239 240 241
	DRM_INFO("\n");
	DRM_INFO("current dispatch: last=0x%x done=0x%x\n",
		 dev_priv->sarea_priv->last_dispatch,
		 (unsigned int)(MGA_READ(MGA_PRIMADDRESS) -
				dev_priv->primary->offset));
	DRM_INFO("current freelist:\n");

	for (entry = dev_priv->head->next; entry; entry = entry->next) {
		DRM_INFO("   %p   idx=%2d  age=0x%x 0x%06lx\n",
			 entry, entry->buf->idx, entry->age.head,
242
			 (unsigned long)(entry->age.head - dev_priv->primary->offset));
L
Linus Torvalds 已提交
243
	}
D
Dave Airlie 已提交
244
	DRM_INFO("\n");
L
Linus Torvalds 已提交
245 246 247
}
#endif

248
static int mga_freelist_init(struct drm_device * dev, drm_mga_private_t * dev_priv)
L
Linus Torvalds 已提交
249
{
250
	struct drm_device_dma *dma = dev->dma;
D
Dave Airlie 已提交
251
	struct drm_buf *buf;
L
Linus Torvalds 已提交
252 253 254
	drm_mga_buf_priv_t *buf_priv;
	drm_mga_freelist_t *entry;
	int i;
D
Dave Airlie 已提交
255
	DRM_DEBUG("count=%d\n", dma->buf_count);
L
Linus Torvalds 已提交
256

D
Dave Airlie 已提交
257 258
	dev_priv->head = drm_alloc(sizeof(drm_mga_freelist_t), DRM_MEM_DRIVER);
	if (dev_priv->head == NULL)
E
Eric Anholt 已提交
259
		return -ENOMEM;
L
Linus Torvalds 已提交
260

D
Dave Airlie 已提交
261 262
	memset(dev_priv->head, 0, sizeof(drm_mga_freelist_t));
	SET_AGE(&dev_priv->head->age, MGA_BUFFER_USED, 0);
L
Linus Torvalds 已提交
263

D
Dave Airlie 已提交
264
	for (i = 0; i < dma->buf_count; i++) {
L
Linus Torvalds 已提交
265
		buf = dma->buflist[i];
D
Dave Airlie 已提交
266
		buf_priv = buf->dev_private;
L
Linus Torvalds 已提交
267

D
Dave Airlie 已提交
268 269
		entry = drm_alloc(sizeof(drm_mga_freelist_t), DRM_MEM_DRIVER);
		if (entry == NULL)
E
Eric Anholt 已提交
270
			return -ENOMEM;
L
Linus Torvalds 已提交
271

D
Dave Airlie 已提交
272
		memset(entry, 0, sizeof(drm_mga_freelist_t));
L
Linus Torvalds 已提交
273 274 275

		entry->next = dev_priv->head->next;
		entry->prev = dev_priv->head;
D
Dave Airlie 已提交
276
		SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
L
Linus Torvalds 已提交
277 278
		entry->buf = buf;

D
Dave Airlie 已提交
279
		if (dev_priv->head->next != NULL)
L
Linus Torvalds 已提交
280
			dev_priv->head->next->prev = entry;
D
Dave Airlie 已提交
281
		if (entry->next == NULL)
L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289 290 291 292 293
			dev_priv->tail = entry;

		buf_priv->list_entry = entry;
		buf_priv->discard = 0;
		buf_priv->dispatched = 0;

		dev_priv->head->next = entry;
	}

	return 0;
}

294
static void mga_freelist_cleanup(struct drm_device * dev)
L
Linus Torvalds 已提交
295 296 297 298
{
	drm_mga_private_t *dev_priv = dev->dev_private;
	drm_mga_freelist_t *entry;
	drm_mga_freelist_t *next;
D
Dave Airlie 已提交
299
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
300 301

	entry = dev_priv->head;
D
Dave Airlie 已提交
302
	while (entry) {
L
Linus Torvalds 已提交
303
		next = entry->next;
D
Dave Airlie 已提交
304
		drm_free(entry, sizeof(drm_mga_freelist_t), DRM_MEM_DRIVER);
L
Linus Torvalds 已提交
305 306 307 308 309 310 311 312 313
		entry = next;
	}

	dev_priv->head = dev_priv->tail = NULL;
}

#if 0
/* FIXME: Still needed?
 */
314
static void mga_freelist_reset(struct drm_device * dev)
L
Linus Torvalds 已提交
315
{
316
	struct drm_device_dma *dma = dev->dma;
D
Dave Airlie 已提交
317
	struct drm_buf *buf;
L
Linus Torvalds 已提交
318 319 320
	drm_mga_buf_priv_t *buf_priv;
	int i;

D
Dave Airlie 已提交
321
	for (i = 0; i < dma->buf_count; i++) {
L
Linus Torvalds 已提交
322
		buf = dma->buflist[i];
D
Dave Airlie 已提交
323 324
		buf_priv = buf->dev_private;
		SET_AGE(&buf_priv->list_entry->age, MGA_BUFFER_FREE, 0);
L
Linus Torvalds 已提交
325 326 327 328
	}
}
#endif

D
Dave Airlie 已提交
329
static struct drm_buf *mga_freelist_get(struct drm_device * dev)
L
Linus Torvalds 已提交
330 331 332 333 334 335
{
	drm_mga_private_t *dev_priv = dev->dev_private;
	drm_mga_freelist_t *next;
	drm_mga_freelist_t *prev;
	drm_mga_freelist_t *tail = dev_priv->tail;
	u32 head, wrap;
D
Dave Airlie 已提交
336
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
337

D
Dave Airlie 已提交
338
	head = MGA_READ(MGA_PRIMADDRESS);
L
Linus Torvalds 已提交
339 340
	wrap = dev_priv->sarea_priv->last_wrap;

D
Dave Airlie 已提交
341 342
	DRM_DEBUG("   tail=0x%06lx %d\n",
		  tail->age.head ?
343
		  (unsigned long)(tail->age.head - dev_priv->primary->offset) : 0,
D
Dave Airlie 已提交
344 345
		  tail->age.wrap);
	DRM_DEBUG("   head=0x%06lx %d\n",
346
		  (unsigned long)(head - dev_priv->primary->offset), wrap);
L
Linus Torvalds 已提交
347

D
Dave Airlie 已提交
348
	if (TEST_AGE(&tail->age, head, wrap)) {
L
Linus Torvalds 已提交
349 350 351 352 353
		prev = dev_priv->tail->prev;
		next = dev_priv->tail;
		prev->next = NULL;
		next->prev = next->next = NULL;
		dev_priv->tail = prev;
D
Dave Airlie 已提交
354
		SET_AGE(&next->age, MGA_BUFFER_USED, 0);
L
Linus Torvalds 已提交
355 356 357
		return next->buf;
	}

D
Dave Airlie 已提交
358
	DRM_DEBUG("returning NULL!\n");
L
Linus Torvalds 已提交
359 360 361
	return NULL;
}

D
Dave Airlie 已提交
362
int mga_freelist_put(struct drm_device * dev, struct drm_buf * buf)
L
Linus Torvalds 已提交
363 364 365 366 367
{
	drm_mga_private_t *dev_priv = dev->dev_private;
	drm_mga_buf_priv_t *buf_priv = buf->dev_private;
	drm_mga_freelist_t *head, *entry, *prev;

D
Dave Airlie 已提交
368
	DRM_DEBUG("age=0x%06lx wrap=%d\n",
369 370 371
		  (unsigned long)(buf_priv->list_entry->age.head -
				  dev_priv->primary->offset),
		  buf_priv->list_entry->age.wrap);
L
Linus Torvalds 已提交
372 373 374 375

	entry = buf_priv->list_entry;
	head = dev_priv->head;

D
Dave Airlie 已提交
376 377
	if (buf_priv->list_entry->age.head == MGA_BUFFER_USED) {
		SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
		prev = dev_priv->tail;
		prev->next = entry;
		entry->prev = prev;
		entry->next = NULL;
	} else {
		prev = head->next;
		head->next = entry;
		prev->prev = entry;
		entry->prev = head;
		entry->next = prev;
	}

	return 0;
}

/* ================================================================
 * DMA initialization, cleanup
 */

397
int mga_driver_load(struct drm_device * dev, unsigned long flags)
398
{
D
Dave Airlie 已提交
399
	drm_mga_private_t *dev_priv;
400
	int ret;
401 402 403

	dev_priv = drm_alloc(sizeof(drm_mga_private_t), DRM_MEM_DRIVER);
	if (!dev_priv)
E
Eric Anholt 已提交
404
		return -ENOMEM;
405 406 407 408 409 410 411

	dev->dev_private = (void *)dev_priv;
	memset(dev_priv, 0, sizeof(drm_mga_private_t));

	dev_priv->usec_timeout = MGA_DEFAULT_USEC_TIMEOUT;
	dev_priv->chipset = flags;

412 413 414 415 416 417 418 419
	dev_priv->mmio_base = drm_get_resource_start(dev, 1);
	dev_priv->mmio_size = drm_get_resource_len(dev, 1);

	dev->counters += 3;
	dev->types[6] = _DRM_STAT_IRQ;
	dev->types[7] = _DRM_STAT_PRIMARY;
	dev->types[8] = _DRM_STAT_SECONDARY;

420 421 422 423 424 425 426
	ret = drm_vblank_init(dev, 1);

	if (ret) {
		(void) mga_driver_unload(dev);
		return ret;
	}

427 428 429
	return 0;
}

D
Dave Airlie 已提交
430
#if __OS_HAS_AGP
431 432
/**
 * Bootstrap the driver for AGP DMA.
D
Dave Airlie 已提交
433
 *
434 435 436 437 438 439 440 441 442 443 444 445
 * \todo
 * Investigate whether there is any benifit to storing the WARP microcode in
 * AGP memory.  If not, the microcode may as well always be put in PCI
 * memory.
 *
 * \todo
 * This routine needs to set dma_bs->agp_mode to the mode actually configured
 * in the hardware.  Looking just at the Linux AGP driver code, I don't see
 * an easy way to determine this.
 *
 * \sa mga_do_dma_bootstrap, mga_do_pci_dma_bootstrap
 */
446
static int mga_do_agp_dma_bootstrap(struct drm_device * dev,
447 448
				    drm_mga_dma_bootstrap_t * dma_bs)
{
D
Dave Airlie 已提交
449 450
	drm_mga_private_t *const dev_priv =
	    (drm_mga_private_t *) dev->dev_private;
451
	unsigned int warp_size = mga_warp_microcode_size(dev_priv);
452
	int err;
D
Dave Airlie 已提交
453
	unsigned offset;
454
	const unsigned secondary_size = dma_bs->secondary_bin_count
D
Dave Airlie 已提交
455
	    * dma_bs->secondary_bin_size;
456
	const unsigned agp_size = (dma_bs->agp_size << 20);
457 458 459 460 461
	struct drm_buf_desc req;
	struct drm_agp_mode mode;
	struct drm_agp_info info;
	struct drm_agp_buffer agp_req;
	struct drm_agp_binding bind_req;
462 463 464 465

	/* Acquire AGP. */
	err = drm_agp_acquire(dev);
	if (err) {
466
		DRM_ERROR("Unable to acquire AGP: %d\n", err);
467 468 469 470 471
		return err;
	}

	err = drm_agp_info(dev, &info);
	if (err) {
472
		DRM_ERROR("Unable to get AGP info: %d\n", err);
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
		return err;
	}

	mode.mode = (info.mode & ~0x07) | dma_bs->agp_mode;
	err = drm_agp_enable(dev, mode);
	if (err) {
		DRM_ERROR("Unable to enable AGP (mode = 0x%lx)\n", mode.mode);
		return err;
	}

	/* In addition to the usual AGP mode configuration, the G200 AGP cards
	 * need to have the AGP mode "manually" set.
	 */

	if (dev_priv->chipset == MGA_CARD_TYPE_G200) {
		if (mode.mode & 0x02) {
			MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_ENABLE);
D
Dave Airlie 已提交
490
		} else {
491 492 493 494 495
			MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_DISABLE);
		}
	}

	/* Allocate and bind AGP memory. */
496 497 498 499 500
	agp_req.size = agp_size;
	agp_req.type = 0;
	err = drm_agp_alloc(dev, &agp_req);
	if (err) {
		dev_priv->agp_size = 0;
501 502
		DRM_ERROR("Unable to allocate %uMB AGP memory\n",
			  dma_bs->agp_size);
503
		return err;
504
	}
D
Dave Airlie 已提交
505

506 507
	dev_priv->agp_size = agp_size;
	dev_priv->agp_handle = agp_req.handle;
D
Dave Airlie 已提交
508

509 510 511
	bind_req.handle = agp_req.handle;
	bind_req.offset = 0;
	err = drm_agp_bind(dev, &bind_req);
512
	if (err) {
513
		DRM_ERROR("Unable to bind AGP memory: %d\n", err);
514 515 516
		return err;
	}

517 518 519 520 521 522
	/* Make drm_addbufs happy by not trying to create a mapping for less
	 * than a page.
	 */
	if (warp_size < PAGE_SIZE)
		warp_size = PAGE_SIZE;

523
	offset = 0;
D
Dave Airlie 已提交
524 525
	err = drm_addmap(dev, offset, warp_size,
			 _DRM_AGP, _DRM_READ_ONLY, &dev_priv->warp);
526
	if (err) {
527
		DRM_ERROR("Unable to map WARP microcode: %d\n", err);
528 529 530 531
		return err;
	}

	offset += warp_size;
D
Dave Airlie 已提交
532 533
	err = drm_addmap(dev, offset, dma_bs->primary_size,
			 _DRM_AGP, _DRM_READ_ONLY, &dev_priv->primary);
534
	if (err) {
535
		DRM_ERROR("Unable to map primary DMA region: %d\n", err);
536 537 538 539
		return err;
	}

	offset += dma_bs->primary_size;
D
Dave Airlie 已提交
540 541
	err = drm_addmap(dev, offset, secondary_size,
			 _DRM_AGP, 0, &dev->agp_buffer_map);
542
	if (err) {
543
		DRM_ERROR("Unable to map secondary DMA region: %d\n", err);
544 545 546
		return err;
	}

D
Dave Airlie 已提交
547
	(void)memset(&req, 0, sizeof(req));
548 549 550 551 552
	req.count = dma_bs->secondary_bin_count;
	req.size = dma_bs->secondary_bin_size;
	req.flags = _DRM_AGP_BUFFER;
	req.agp_start = offset;

D
Dave Airlie 已提交
553
	err = drm_addbufs_agp(dev, &req);
554
	if (err) {
555
		DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
556 557 558
		return err;
	}

559
	{
D
Dave Airlie 已提交
560
		struct drm_map_list *_entry;
561
		unsigned long agp_token = 0;
D
Dave Airlie 已提交
562

563
		list_for_each_entry(_entry, &dev->maplist, head) {
564 565 566 567 568 569 570 571 572
			if (_entry->map == dev->agp_buffer_map)
				agp_token = _entry->user_token;
		}
		if (!agp_token)
			return -EFAULT;

		dev->agp_buffer_token = agp_token;
	}

573
	offset += secondary_size;
D
Dave Airlie 已提交
574 575
	err = drm_addmap(dev, offset, agp_size - offset,
			 _DRM_AGP, 0, &dev_priv->agp_textures);
576
	if (err) {
577
		DRM_ERROR("Unable to map AGP texture region %d\n", err);
578 579 580 581 582 583 584 585 586 587 588 589
		return err;
	}

	drm_core_ioremap(dev_priv->warp, dev);
	drm_core_ioremap(dev_priv->primary, dev);
	drm_core_ioremap(dev->agp_buffer_map, dev);

	if (!dev_priv->warp->handle ||
	    !dev_priv->primary->handle || !dev->agp_buffer_map->handle) {
		DRM_ERROR("failed to ioremap agp regions! (%p, %p, %p)\n",
			  dev_priv->warp->handle, dev_priv->primary->handle,
			  dev->agp_buffer_map->handle);
E
Eric Anholt 已提交
590
		return -ENOMEM;
591 592 593 594 595 596 597 598
	}

	dev_priv->dma_access = MGA_PAGPXFER;
	dev_priv->wagp_enable = MGA_WAGP_ENABLE;

	DRM_INFO("Initialized card for AGP DMA.\n");
	return 0;
}
D
Dave Airlie 已提交
599
#else
600
static int mga_do_agp_dma_bootstrap(struct drm_device * dev,
D
Dave Airlie 已提交
601 602 603 604 605
				    drm_mga_dma_bootstrap_t * dma_bs)
{
	return -EINVAL;
}
#endif
606 607 608

/**
 * Bootstrap the driver for PCI DMA.
D
Dave Airlie 已提交
609
 *
610 611 612 613 614 615 616 617
 * \todo
 * The algorithm for decreasing the size of the primary DMA buffer could be
 * better.  The size should be rounded up to the nearest page size, then
 * decrease the request size by a single page each pass through the loop.
 *
 * \todo
 * Determine whether the maximum address passed to drm_pci_alloc is correct.
 * The same goes for drm_addbufs_pci.
D
Dave Airlie 已提交
618
 *
619 620
 * \sa mga_do_dma_bootstrap, mga_do_agp_dma_bootstrap
 */
621
static int mga_do_pci_dma_bootstrap(struct drm_device * dev,
622 623
				    drm_mga_dma_bootstrap_t * dma_bs)
{
D
Dave Airlie 已提交
624 625
	drm_mga_private_t *const dev_priv =
	    (drm_mga_private_t *) dev->dev_private;
626
	unsigned int warp_size = mga_warp_microcode_size(dev_priv);
627 628 629
	unsigned int primary_size;
	unsigned int bin_count;
	int err;
630
	struct drm_buf_desc req;
631 632 633

	if (dev->dma == NULL) {
		DRM_ERROR("dev->dma is NULL\n");
E
Eric Anholt 已提交
634
		return -EFAULT;
635 636
	}

637 638 639 640 641 642
	/* Make drm_addbufs happy by not trying to create a mapping for less
	 * than a page.
	 */
	if (warp_size < PAGE_SIZE)
		warp_size = PAGE_SIZE;

643 644 645 646
	/* The proper alignment is 0x100 for this mapping */
	err = drm_addmap(dev, 0, warp_size, _DRM_CONSISTENT,
			 _DRM_READ_ONLY, &dev_priv->warp);
	if (err != 0) {
647 648
		DRM_ERROR("Unable to create mapping for WARP microcode: %d\n",
			  err);
649 650 651 652 653 654 655 656
		return err;
	}

	/* Other than the bottom two bits being used to encode other
	 * information, there don't appear to be any restrictions on the
	 * alignment of the primary or secondary DMA buffers.
	 */

D
Dave Airlie 已提交
657 658
	for (primary_size = dma_bs->primary_size; primary_size != 0;
	     primary_size >>= 1) {
659 660 661 662 663 664 665 666
		/* The proper alignment for this mapping is 0x04 */
		err = drm_addmap(dev, 0, primary_size, _DRM_CONSISTENT,
				 _DRM_READ_ONLY, &dev_priv->primary);
		if (!err)
			break;
	}

	if (err != 0) {
667
		DRM_ERROR("Unable to allocate primary DMA region: %d\n", err);
E
Eric Anholt 已提交
668
		return -ENOMEM;
669 670 671 672
	}

	if (dev_priv->primary->size != dma_bs->primary_size) {
		DRM_INFO("Primary DMA buffer size reduced from %u to %u.\n",
D
Dave Airlie 已提交
673 674
			 dma_bs->primary_size,
			 (unsigned)dev_priv->primary->size);
675 676 677
		dma_bs->primary_size = dev_priv->primary->size;
	}

D
Dave Airlie 已提交
678 679 680
	for (bin_count = dma_bs->secondary_bin_count; bin_count > 0;
	     bin_count--) {
		(void)memset(&req, 0, sizeof(req));
681 682 683
		req.count = bin_count;
		req.size = dma_bs->secondary_bin_size;

D
Dave Airlie 已提交
684
		err = drm_addbufs_pci(dev, &req);
685 686 687 688
		if (!err) {
			break;
		}
	}
D
Dave Airlie 已提交
689

690
	if (bin_count == 0) {
691
		DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
		return err;
	}

	if (bin_count != dma_bs->secondary_bin_count) {
		DRM_INFO("Secondary PCI DMA buffer bin count reduced from %u "
			 "to %u.\n", dma_bs->secondary_bin_count, bin_count);

		dma_bs->secondary_bin_count = bin_count;
	}

	dev_priv->dma_access = 0;
	dev_priv->wagp_enable = 0;

	dma_bs->agp_mode = 0;

	DRM_INFO("Initialized card for PCI DMA.\n");
	return 0;
}

711
static int mga_do_dma_bootstrap(struct drm_device * dev,
712 713 714 715
				drm_mga_dma_bootstrap_t * dma_bs)
{
	const int is_agp = (dma_bs->agp_mode != 0) && drm_device_is_agp(dev);
	int err;
D
Dave Airlie 已提交
716 717
	drm_mga_private_t *const dev_priv =
	    (drm_mga_private_t *) dev->dev_private;
718 719 720 721 722 723

	dev_priv->used_new_dma_init = 1;

	/* The first steps are the same for both PCI and AGP based DMA.  Map
	 * the cards MMIO registers and map a status page.
	 */
D
Dave Airlie 已提交
724 725
	err = drm_addmap(dev, dev_priv->mmio_base, dev_priv->mmio_size,
			 _DRM_REGISTERS, _DRM_READ_ONLY, &dev_priv->mmio);
726
	if (err) {
727
		DRM_ERROR("Unable to map MMIO region: %d\n", err);
728 729 730
		return err;
	}

D
Dave Airlie 已提交
731 732 733
	err = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
			 _DRM_READ_ONLY | _DRM_LOCKED | _DRM_KERNEL,
			 &dev_priv->status);
734
	if (err) {
735
		DRM_ERROR("Unable to map status region: %d\n", err);
736 737 738 739 740 741 742 743 744 745 746
		return err;
	}

	/* The DMA initialization procedure is slightly different for PCI and
	 * AGP cards.  AGP cards just allocate a large block of AGP memory and
	 * carve off portions of it for internal uses.  The remaining memory
	 * is returned to user-mode to be used for AGP textures.
	 */
	if (is_agp) {
		err = mga_do_agp_dma_bootstrap(dev, dma_bs);
	}
D
Dave Airlie 已提交
747

748 749 750 751 752
	/* If we attempted to initialize the card for AGP DMA but failed,
	 * clean-up any mess that may have been created.
	 */

	if (err) {
753
		mga_do_cleanup_dma(dev, MINIMAL_CLEANUP);
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
	}

	/* Not only do we want to try and initialized PCI cards for PCI DMA,
	 * but we also try to initialized AGP cards that could not be
	 * initialized for AGP DMA.  This covers the case where we have an AGP
	 * card in a system with an unsupported AGP chipset.  In that case the
	 * card will be detected as AGP, but we won't be able to allocate any
	 * AGP memory, etc.
	 */

	if (!is_agp || err) {
		err = mga_do_pci_dma_bootstrap(dev, dma_bs);
	}

	return err;
}

771 772
int mga_dma_bootstrap(struct drm_device *dev, void *data,
		      struct drm_file *file_priv)
773
{
774
	drm_mga_dma_bootstrap_t *bootstrap = data;
775
	int err;
776 777 778
	static const int modes[] = { 0, 1, 2, 2, 4, 4, 4, 4 };
	const drm_mga_private_t *const dev_priv =
		(drm_mga_private_t *) dev->dev_private;
779

780
	err = mga_do_dma_bootstrap(dev, bootstrap);
781 782 783 784
	if (err) {
		mga_do_cleanup_dma(dev, FULL_CLEANUP);
		return err;
	}
785

786
	if (dev_priv->agp_textures != NULL) {
787 788
		bootstrap->texture_handle = dev_priv->agp_textures->offset;
		bootstrap->texture_size = dev_priv->agp_textures->size;
D
Dave Airlie 已提交
789
	} else {
790 791
		bootstrap->texture_handle = 0;
		bootstrap->texture_size = 0;
792 793
	}

794
	bootstrap->agp_mode = modes[bootstrap->agp_mode & 0x07];
795

796 797 798
	return err;
}

799
static int mga_do_init_dma(struct drm_device * dev, drm_mga_init_t * init)
L
Linus Torvalds 已提交
800 801 802
{
	drm_mga_private_t *dev_priv;
	int ret;
D
Dave Airlie 已提交
803
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
804

805
	dev_priv = dev->dev_private;
L
Linus Torvalds 已提交
806

807
	if (init->sgram) {
L
Linus Torvalds 已提交
808 809 810 811
		dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_BLK;
	} else {
		dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_RSTR;
	}
D
Dave Airlie 已提交
812
	dev_priv->maccess = init->maccess;
L
Linus Torvalds 已提交
813

D
Dave Airlie 已提交
814 815 816 817 818
	dev_priv->fb_cpp = init->fb_cpp;
	dev_priv->front_offset = init->front_offset;
	dev_priv->front_pitch = init->front_pitch;
	dev_priv->back_offset = init->back_offset;
	dev_priv->back_pitch = init->back_pitch;
L
Linus Torvalds 已提交
819

D
Dave Airlie 已提交
820 821 822
	dev_priv->depth_cpp = init->depth_cpp;
	dev_priv->depth_offset = init->depth_offset;
	dev_priv->depth_pitch = init->depth_pitch;
L
Linus Torvalds 已提交
823 824 825 826 827 828

	/* FIXME: Need to support AGP textures...
	 */
	dev_priv->texture_offset = init->texture_offset[0];
	dev_priv->texture_size = init->texture_size[0];

829
	dev_priv->sarea = drm_getsarea(dev);
830 831
	if (!dev_priv->sarea) {
		DRM_ERROR("failed to find sarea!\n");
E
Eric Anholt 已提交
832
		return -EINVAL;
L
Linus Torvalds 已提交
833 834
	}

D
Dave Airlie 已提交
835
	if (!dev_priv->used_new_dma_init) {
836 837 838 839

		dev_priv->dma_access = MGA_PAGPXFER;
		dev_priv->wagp_enable = MGA_WAGP_ENABLE;

840 841 842
		dev_priv->status = drm_core_findmap(dev, init->status_offset);
		if (!dev_priv->status) {
			DRM_ERROR("failed to find status page!\n");
E
Eric Anholt 已提交
843
			return -EINVAL;
844 845 846 847
		}
		dev_priv->mmio = drm_core_findmap(dev, init->mmio_offset);
		if (!dev_priv->mmio) {
			DRM_ERROR("failed to find mmio region!\n");
E
Eric Anholt 已提交
848
			return -EINVAL;
849 850 851 852
		}
		dev_priv->warp = drm_core_findmap(dev, init->warp_offset);
		if (!dev_priv->warp) {
			DRM_ERROR("failed to find warp microcode region!\n");
E
Eric Anholt 已提交
853
			return -EINVAL;
854 855 856 857
		}
		dev_priv->primary = drm_core_findmap(dev, init->primary_offset);
		if (!dev_priv->primary) {
			DRM_ERROR("failed to find primary dma region!\n");
E
Eric Anholt 已提交
858
			return -EINVAL;
859
		}
860
		dev->agp_buffer_token = init->buffers_offset;
D
Dave Airlie 已提交
861 862
		dev->agp_buffer_map =
		    drm_core_findmap(dev, init->buffers_offset);
863 864
		if (!dev->agp_buffer_map) {
			DRM_ERROR("failed to find dma buffer region!\n");
E
Eric Anholt 已提交
865
			return -EINVAL;
866 867 868 869 870
		}

		drm_core_ioremap(dev_priv->warp, dev);
		drm_core_ioremap(dev_priv->primary, dev);
		drm_core_ioremap(dev->agp_buffer_map, dev);
L
Linus Torvalds 已提交
871 872 873
	}

	dev_priv->sarea_priv =
D
Dave Airlie 已提交
874 875
	    (drm_mga_sarea_t *) ((u8 *) dev_priv->sarea->handle +
				 init->sarea_priv_offset);
L
Linus Torvalds 已提交
876

877 878 879 880 881 882
	if (!dev_priv->warp->handle ||
	    !dev_priv->primary->handle ||
	    ((dev_priv->dma_access != 0) &&
	     ((dev->agp_buffer_map == NULL) ||
	      (dev->agp_buffer_map->handle == NULL)))) {
		DRM_ERROR("failed to ioremap agp regions!\n");
E
Eric Anholt 已提交
883
		return -ENOMEM;
L
Linus Torvalds 已提交
884 885
	}

886 887
	ret = mga_warp_install_microcode(dev_priv);
	if (ret < 0) {
888
		DRM_ERROR("failed to install WARP ucode!: %d\n", ret);
L
Linus Torvalds 已提交
889 890 891
		return ret;
	}

892 893
	ret = mga_warp_init(dev_priv);
	if (ret < 0) {
894
		DRM_ERROR("failed to init WARP engine!: %d\n", ret);
L
Linus Torvalds 已提交
895 896 897
		return ret;
	}

D
Dave Airlie 已提交
898
	dev_priv->prim.status = (u32 *) dev_priv->status->handle;
L
Linus Torvalds 已提交
899

D
Dave Airlie 已提交
900
	mga_do_wait_for_idle(dev_priv);
L
Linus Torvalds 已提交
901 902 903

	/* Init the primary DMA registers.
	 */
D
Dave Airlie 已提交
904
	MGA_WRITE(MGA_PRIMADDRESS, dev_priv->primary->offset | MGA_DMA_GENERAL);
L
Linus Torvalds 已提交
905
#if 0
D
Dave Airlie 已提交
906 907
	MGA_WRITE(MGA_PRIMPTR, virt_to_bus((void *)dev_priv->prim.status) | MGA_PRIMPTREN0 |	/* Soft trap, SECEND, SETUPEND */
		  MGA_PRIMPTREN1);	/* DWGSYNC */
L
Linus Torvalds 已提交
908 909
#endif

D
Dave Airlie 已提交
910 911
	dev_priv->prim.start = (u8 *) dev_priv->primary->handle;
	dev_priv->prim.end = ((u8 *) dev_priv->primary->handle
L
Linus Torvalds 已提交
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
			      + dev_priv->primary->size);
	dev_priv->prim.size = dev_priv->primary->size;

	dev_priv->prim.tail = 0;
	dev_priv->prim.space = dev_priv->prim.size;
	dev_priv->prim.wrapped = 0;

	dev_priv->prim.last_flush = 0;
	dev_priv->prim.last_wrap = 0;

	dev_priv->prim.high_mark = 256 * DMA_BLOCK_SIZE;

	dev_priv->prim.status[0] = dev_priv->primary->offset;
	dev_priv->prim.status[1] = 0;

	dev_priv->sarea_priv->last_wrap = 0;
	dev_priv->sarea_priv->last_frame.head = 0;
	dev_priv->sarea_priv->last_frame.wrap = 0;

931 932
	if (mga_freelist_init(dev, dev_priv) < 0) {
		DRM_ERROR("could not initialize freelist\n");
E
Eric Anholt 已提交
933
		return -ENOMEM;
L
Linus Torvalds 已提交
934 935 936 937 938
	}

	return 0;
}

939
static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup)
L
Linus Torvalds 已提交
940
{
941 942
	int err = 0;
	DRM_DEBUG("\n");
L
Linus Torvalds 已提交
943 944 945 946 947

	/* Make sure interrupts are disabled here because the uninstall ioctl
	 * may not have been called from userspace and after dev_private
	 * is freed, it's too late.
	 */
D
Dave Airlie 已提交
948 949
	if (dev->irq_enabled)
		drm_irq_uninstall(dev);
L
Linus Torvalds 已提交
950

D
Dave Airlie 已提交
951
	if (dev->dev_private) {
L
Linus Torvalds 已提交
952 953
		drm_mga_private_t *dev_priv = dev->dev_private;

D
Dave Airlie 已提交
954
		if ((dev_priv->warp != NULL)
955
		    && (dev_priv->warp->type != _DRM_CONSISTENT))
956 957
			drm_core_ioremapfree(dev_priv->warp, dev);

D
Dave Airlie 已提交
958
		if ((dev_priv->primary != NULL)
959 960
		    && (dev_priv->primary->type != _DRM_CONSISTENT))
			drm_core_ioremapfree(dev_priv->primary, dev);
L
Linus Torvalds 已提交
961

962 963 964 965
		if (dev->agp_buffer_map != NULL)
			drm_core_ioremapfree(dev->agp_buffer_map, dev);

		if (dev_priv->used_new_dma_init) {
D
Dave Airlie 已提交
966
#if __OS_HAS_AGP
967
			if (dev_priv->agp_handle != 0) {
968 969
				struct drm_agp_binding unbind_req;
				struct drm_agp_buffer free_req;
970

971 972 973 974 975
				unbind_req.handle = dev_priv->agp_handle;
				drm_agp_unbind(dev, &unbind_req);

				free_req.handle = dev_priv->agp_handle;
				drm_agp_free(dev, &free_req);
D
Dave Airlie 已提交
976

977 978 979
				dev_priv->agp_textures = NULL;
				dev_priv->agp_size = 0;
				dev_priv->agp_handle = 0;
980 981 982 983 984
			}

			if ((dev->agp != NULL) && dev->agp->acquired) {
				err = drm_agp_release(dev);
			}
D
Dave Airlie 已提交
985
#endif
L
Linus Torvalds 已提交
986 987
		}

988 989 990 991 992 993
		dev_priv->warp = NULL;
		dev_priv->primary = NULL;
		dev_priv->sarea = NULL;
		dev_priv->sarea_priv = NULL;
		dev->agp_buffer_map = NULL;

994 995 996 997 998 999
		if (full_cleanup) {
			dev_priv->mmio = NULL;
			dev_priv->status = NULL;
			dev_priv->used_new_dma_init = 0;
		}

1000 1001
		memset(&dev_priv->prim, 0, sizeof(dev_priv->prim));
		dev_priv->warp_pipe = 0;
D
Dave Airlie 已提交
1002 1003
		memset(dev_priv->warp_pipe_phys, 0,
		       sizeof(dev_priv->warp_pipe_phys));
1004 1005 1006 1007

		if (dev_priv->head != NULL) {
			mga_freelist_cleanup(dev);
		}
L
Linus Torvalds 已提交
1008 1009
	}

1010
	return err;
L
Linus Torvalds 已提交
1011 1012
}

1013 1014
int mga_dma_init(struct drm_device *dev, void *data,
		 struct drm_file *file_priv)
L
Linus Torvalds 已提交
1015
{
1016
	drm_mga_init_t *init = data;
1017
	int err;
L
Linus Torvalds 已提交
1018

1019
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
1020

1021
	switch (init->func) {
L
Linus Torvalds 已提交
1022
	case MGA_INIT_DMA:
1023
		err = mga_do_init_dma(dev, init);
1024
		if (err) {
1025
			(void)mga_do_cleanup_dma(dev, FULL_CLEANUP);
1026 1027
		}
		return err;
L
Linus Torvalds 已提交
1028
	case MGA_CLEANUP_DMA:
1029
		return mga_do_cleanup_dma(dev, FULL_CLEANUP);
L
Linus Torvalds 已提交
1030 1031
	}

E
Eric Anholt 已提交
1032
	return -EINVAL;
L
Linus Torvalds 已提交
1033 1034 1035 1036 1037 1038
}

/* ================================================================
 * Primary DMA stream management
 */

1039 1040
int mga_dma_flush(struct drm_device *dev, void *data,
		  struct drm_file *file_priv)
L
Linus Torvalds 已提交
1041
{
D
Dave Airlie 已提交
1042
	drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
1043
	struct drm_lock *lock = data;
L
Linus Torvalds 已提交
1044

1045
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
1046

D
Dave Airlie 已提交
1047
	DRM_DEBUG("%s%s%s\n",
1048 1049 1050
		  (lock->flags & _DRM_LOCK_FLUSH) ? "flush, " : "",
		  (lock->flags & _DRM_LOCK_FLUSH_ALL) ? "flush all, " : "",
		  (lock->flags & _DRM_LOCK_QUIESCENT) ? "idle, " : "");
L
Linus Torvalds 已提交
1051

D
Dave Airlie 已提交
1052
	WRAP_WAIT_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
1053

1054
	if (lock->flags & (_DRM_LOCK_FLUSH | _DRM_LOCK_FLUSH_ALL)) {
D
Dave Airlie 已提交
1055
		mga_do_dma_flush(dev_priv);
L
Linus Torvalds 已提交
1056 1057
	}

1058
	if (lock->flags & _DRM_LOCK_QUIESCENT) {
L
Linus Torvalds 已提交
1059
#if MGA_DMA_DEBUG
D
Dave Airlie 已提交
1060 1061
		int ret = mga_do_wait_for_idle(dev_priv);
		if (ret < 0)
1062
			DRM_INFO("-EBUSY\n");
L
Linus Torvalds 已提交
1063 1064
		return ret;
#else
D
Dave Airlie 已提交
1065
		return mga_do_wait_for_idle(dev_priv);
L
Linus Torvalds 已提交
1066 1067 1068 1069 1070 1071
#endif
	} else {
		return 0;
	}
}

1072 1073
int mga_dma_reset(struct drm_device *dev, void *data,
		  struct drm_file *file_priv)
L
Linus Torvalds 已提交
1074
{
D
Dave Airlie 已提交
1075
	drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
L
Linus Torvalds 已提交
1076

1077
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
1078

D
Dave Airlie 已提交
1079
	return mga_do_dma_reset(dev_priv);
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084 1085
}

/* ================================================================
 * DMA buffer management
 */

1086 1087
static int mga_dma_get_buffers(struct drm_device * dev,
			       struct drm_file *file_priv, struct drm_dma * d)
L
Linus Torvalds 已提交
1088
{
D
Dave Airlie 已提交
1089
	struct drm_buf *buf;
L
Linus Torvalds 已提交
1090 1091
	int i;

D
Dave Airlie 已提交
1092 1093 1094
	for (i = d->granted_count; i < d->request_count; i++) {
		buf = mga_freelist_get(dev);
		if (!buf)
E
Eric Anholt 已提交
1095
			return -EAGAIN;
L
Linus Torvalds 已提交
1096

1097
		buf->file_priv = file_priv;
L
Linus Torvalds 已提交
1098

D
Dave Airlie 已提交
1099 1100
		if (DRM_COPY_TO_USER(&d->request_indices[i],
				     &buf->idx, sizeof(buf->idx)))
E
Eric Anholt 已提交
1101
			return -EFAULT;
D
Dave Airlie 已提交
1102 1103
		if (DRM_COPY_TO_USER(&d->request_sizes[i],
				     &buf->total, sizeof(buf->total)))
E
Eric Anholt 已提交
1104
			return -EFAULT;
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109 1110

		d->granted_count++;
	}
	return 0;
}

1111 1112
int mga_dma_buffers(struct drm_device *dev, void *data,
		    struct drm_file *file_priv)
L
Linus Torvalds 已提交
1113
{
1114
	struct drm_device_dma *dma = dev->dma;
D
Dave Airlie 已提交
1115
	drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
1116
	struct drm_dma *d = data;
L
Linus Torvalds 已提交
1117 1118
	int ret = 0;

1119
	LOCK_TEST_WITH_RETURN(dev, file_priv);
L
Linus Torvalds 已提交
1120 1121 1122

	/* Please don't send us buffers.
	 */
1123
	if (d->send_count != 0) {
D
Dave Airlie 已提交
1124
		DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
1125
			  DRM_CURRENTPID, d->send_count);
E
Eric Anholt 已提交
1126
		return -EINVAL;
L
Linus Torvalds 已提交
1127 1128 1129 1130
	}

	/* We'll send you buffers.
	 */
1131
	if (d->request_count < 0 || d->request_count > dma->buf_count) {
D
Dave Airlie 已提交
1132
		DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
1133
			  DRM_CURRENTPID, d->request_count, dma->buf_count);
E
Eric Anholt 已提交
1134
		return -EINVAL;
L
Linus Torvalds 已提交
1135 1136
	}

D
Dave Airlie 已提交
1137
	WRAP_TEST_WITH_RETURN(dev_priv);
L
Linus Torvalds 已提交
1138

1139
	d->granted_count = 0;
L
Linus Torvalds 已提交
1140

1141 1142
	if (d->request_count) {
		ret = mga_dma_get_buffers(dev, file_priv, d);
L
Linus Torvalds 已提交
1143 1144 1145 1146 1147
	}

	return ret;
}

1148 1149 1150
/**
 * Called just before the module is unloaded.
 */
1151
int mga_driver_unload(struct drm_device * dev)
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
{
	drm_free(dev->dev_private, sizeof(drm_mga_private_t), DRM_MEM_DRIVER);
	dev->dev_private = NULL;

	return 0;
}

/**
 * Called when the last opener of the device is closed.
 */
1162
void mga_driver_lastclose(struct drm_device * dev)
L
Linus Torvalds 已提交
1163
{
1164
	mga_do_cleanup_dma(dev, FULL_CLEANUP);
L
Linus Torvalds 已提交
1165 1166
}

1167
int mga_driver_dma_quiescent(struct drm_device * dev)
L
Linus Torvalds 已提交
1168 1169
{
	drm_mga_private_t *dev_priv = dev->dev_private;
D
Dave Airlie 已提交
1170
	return mga_do_wait_for_idle(dev_priv);
L
Linus Torvalds 已提交
1171
}