savage_bci.c 30.6 KB
Newer Older
D
Dave Airlie 已提交
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 26 27 28 29 30
/* savage_bci.c -- BCI support for Savage
 *
 * Copyright 2004  Felix Kuehling
 * 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, sub license,
 * 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
 * NON-INFRINGEMENT. IN NO EVENT SHALL FELIX KUEHLING 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.
 */
#include "drmP.h"
#include "savage_drm.h"
#include "savage_drv.h"

/* Need a long timeout for shadow status updates can take a while
 * and so can waiting for events when the queue is full. */
D
Dave Airlie 已提交
31 32
#define SAVAGE_DEFAULT_USEC_TIMEOUT	1000000	/* 1s */
#define SAVAGE_EVENT_USEC_TIMEOUT	5000000	/* 5s */
D
Dave Airlie 已提交
33 34
#define SAVAGE_FREELIST_DEBUG		0

35
static int savage_do_cleanup_bci(struct drm_device *dev);
36

D
Dave Airlie 已提交
37
static int
D
Dave Airlie 已提交
38
savage_bci_wait_fifo_shadow(drm_savage_private_t * dev_priv, unsigned int n)
D
Dave Airlie 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
{
	uint32_t mask = dev_priv->status_used_mask;
	uint32_t threshold = dev_priv->bci_threshold_hi;
	uint32_t status;
	int i;

#if SAVAGE_BCI_DEBUG
	if (n > dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - threshold)
		DRM_ERROR("Trying to emit %d words "
			  "(more than guaranteed space in COB)\n", n);
#endif

	for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) {
		DRM_MEMORYBARRIER();
		status = dev_priv->status_ptr[0];
		if ((status & mask) < threshold)
			return 0;
		DRM_UDELAY(1);
	}

#if SAVAGE_BCI_DEBUG
	DRM_ERROR("failed!\n");
	DRM_INFO("   status=0x%08x, threshold=0x%08x\n", status, threshold);
#endif
E
Eric Anholt 已提交
63
	return -EBUSY;
D
Dave Airlie 已提交
64 65 66
}

static int
D
Dave Airlie 已提交
67
savage_bci_wait_fifo_s3d(drm_savage_private_t * dev_priv, unsigned int n)
D
Dave Airlie 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
{
	uint32_t maxUsed = dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - n;
	uint32_t status;
	int i;

	for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) {
		status = SAVAGE_READ(SAVAGE_STATUS_WORD0);
		if ((status & SAVAGE_FIFO_USED_MASK_S3D) <= maxUsed)
			return 0;
		DRM_UDELAY(1);
	}

#if SAVAGE_BCI_DEBUG
	DRM_ERROR("failed!\n");
	DRM_INFO("   status=0x%08x\n", status);
#endif
E
Eric Anholt 已提交
84
	return -EBUSY;
D
Dave Airlie 已提交
85 86 87
}

static int
D
Dave Airlie 已提交
88
savage_bci_wait_fifo_s4(drm_savage_private_t * dev_priv, unsigned int n)
D
Dave Airlie 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
{
	uint32_t maxUsed = dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - n;
	uint32_t status;
	int i;

	for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) {
		status = SAVAGE_READ(SAVAGE_ALT_STATUS_WORD0);
		if ((status & SAVAGE_FIFO_USED_MASK_S4) <= maxUsed)
			return 0;
		DRM_UDELAY(1);
	}

#if SAVAGE_BCI_DEBUG
	DRM_ERROR("failed!\n");
	DRM_INFO("   status=0x%08x\n", status);
#endif
E
Eric Anholt 已提交
105
	return -EBUSY;
D
Dave Airlie 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119
}

/*
 * Waiting for events.
 *
 * The BIOSresets the event tag to 0 on mode changes. Therefore we
 * never emit 0 to the event tag. If we find a 0 event tag we know the
 * BIOS stomped on it and return success assuming that the BIOS waited
 * for engine idle.
 *
 * Note: if the Xserver uses the event tag it has to follow the same
 * rule. Otherwise there may be glitches every 2^16 events.
 */
static int
D
Dave Airlie 已提交
120
savage_bci_wait_event_shadow(drm_savage_private_t * dev_priv, uint16_t e)
D
Dave Airlie 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
{
	uint32_t status;
	int i;

	for (i = 0; i < SAVAGE_EVENT_USEC_TIMEOUT; i++) {
		DRM_MEMORYBARRIER();
		status = dev_priv->status_ptr[1];
		if ((((status & 0xffff) - e) & 0xffff) <= 0x7fff ||
		    (status & 0xffff) == 0)
			return 0;
		DRM_UDELAY(1);
	}

#if SAVAGE_BCI_DEBUG
	DRM_ERROR("failed!\n");
	DRM_INFO("   status=0x%08x, e=0x%04x\n", status, e);
#endif

E
Eric Anholt 已提交
139
	return -EBUSY;
D
Dave Airlie 已提交
140 141 142
}

static int
D
Dave Airlie 已提交
143
savage_bci_wait_event_reg(drm_savage_private_t * dev_priv, uint16_t e)
D
Dave Airlie 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
{
	uint32_t status;
	int i;

	for (i = 0; i < SAVAGE_EVENT_USEC_TIMEOUT; i++) {
		status = SAVAGE_READ(SAVAGE_STATUS_WORD1);
		if ((((status & 0xffff) - e) & 0xffff) <= 0x7fff ||
		    (status & 0xffff) == 0)
			return 0;
		DRM_UDELAY(1);
	}

#if SAVAGE_BCI_DEBUG
	DRM_ERROR("failed!\n");
	DRM_INFO("   status=0x%08x, e=0x%04x\n", status, e);
#endif

E
Eric Anholt 已提交
161
	return -EBUSY;
D
Dave Airlie 已提交
162 163
}

D
Dave Airlie 已提交
164
uint16_t savage_bci_emit_event(drm_savage_private_t * dev_priv,
D
Dave Airlie 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
			       unsigned int flags)
{
	uint16_t count;
	BCI_LOCALS;

	if (dev_priv->status_ptr) {
		/* coordinate with Xserver */
		count = dev_priv->status_ptr[1023];
		if (count < dev_priv->event_counter)
			dev_priv->event_wrap++;
	} else {
		count = dev_priv->event_counter;
	}
	count = (count + 1) & 0xffff;
	if (count == 0) {
D
Dave Airlie 已提交
180
		count++;	/* See the comment above savage_wait_event_*. */
D
Dave Airlie 已提交
181 182 183 184
		dev_priv->event_wrap++;
	}
	dev_priv->event_counter = count;
	if (dev_priv->status_ptr)
D
Dave Airlie 已提交
185
		dev_priv->status_ptr[1023] = (uint32_t) count;
D
Dave Airlie 已提交
186 187 188 189 190 191 192 193 194 195 196 197

	if ((flags & (SAVAGE_WAIT_2D | SAVAGE_WAIT_3D))) {
		unsigned int wait_cmd = BCI_CMD_WAIT;
		if ((flags & SAVAGE_WAIT_2D))
			wait_cmd |= BCI_CMD_WAIT_2D;
		if ((flags & SAVAGE_WAIT_3D))
			wait_cmd |= BCI_CMD_WAIT_3D;
		BEGIN_BCI(2);
		BCI_WRITE(wait_cmd);
	} else {
		BEGIN_BCI(1);
	}
D
Dave Airlie 已提交
198
	BCI_WRITE(BCI_CMD_UPDATE_EVENT_TAG | (uint32_t) count);
D
Dave Airlie 已提交
199 200 201 202 203 204 205

	return count;
}

/*
 * Freelist management
 */
206
static int savage_freelist_init(struct drm_device * dev)
D
Dave Airlie 已提交
207 208
{
	drm_savage_private_t *dev_priv = dev->dev_private;
209
	struct drm_device_dma *dma = dev->dma;
D
Dave Airlie 已提交
210
	struct drm_buf *buf;
D
Dave Airlie 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	drm_savage_buf_priv_t *entry;
	int i;
	DRM_DEBUG("count=%d\n", dma->buf_count);

	dev_priv->head.next = &dev_priv->tail;
	dev_priv->head.prev = NULL;
	dev_priv->head.buf = NULL;

	dev_priv->tail.next = NULL;
	dev_priv->tail.prev = &dev_priv->head;
	dev_priv->tail.buf = NULL;

	for (i = 0; i < dma->buf_count; i++) {
		buf = dma->buflist[i];
		entry = buf->dev_private;

		SET_AGE(&entry->age, 0, 0);
		entry->buf = buf;

		entry->next = dev_priv->head.next;
		entry->prev = &dev_priv->head;
		dev_priv->head.next->prev = entry;
		dev_priv->head.next = entry;
	}

	return 0;
}

D
Dave Airlie 已提交
239
static struct drm_buf *savage_freelist_get(struct drm_device * dev)
D
Dave Airlie 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253
{
	drm_savage_private_t *dev_priv = dev->dev_private;
	drm_savage_buf_priv_t *tail = dev_priv->tail.prev;
	uint16_t event;
	unsigned int wrap;
	DRM_DEBUG("\n");

	UPDATE_EVENT_COUNTER();
	if (dev_priv->status_ptr)
		event = dev_priv->status_ptr[1] & 0xffff;
	else
		event = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff;
	wrap = dev_priv->event_wrap;
	if (event > dev_priv->event_counter)
D
Dave Airlie 已提交
254
		wrap--;		/* hardware hasn't passed the last wrap yet */
D
Dave Airlie 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

	DRM_DEBUG("   tail=0x%04x %d\n", tail->age.event, tail->age.wrap);
	DRM_DEBUG("   head=0x%04x %d\n", event, wrap);

	if (tail->buf && (TEST_AGE(&tail->age, event, wrap) || event == 0)) {
		drm_savage_buf_priv_t *next = tail->next;
		drm_savage_buf_priv_t *prev = tail->prev;
		prev->next = next;
		next->prev = prev;
		tail->next = tail->prev = NULL;
		return tail->buf;
	}

	DRM_DEBUG("returning NULL, tail->buf=%p!\n", tail->buf);
	return NULL;
}

D
Dave Airlie 已提交
272
void savage_freelist_put(struct drm_device * dev, struct drm_buf * buf)
D
Dave Airlie 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
{
	drm_savage_private_t *dev_priv = dev->dev_private;
	drm_savage_buf_priv_t *entry = buf->dev_private, *prev, *next;

	DRM_DEBUG("age=0x%04x wrap=%d\n", entry->age.event, entry->age.wrap);

	if (entry->next != NULL || entry->prev != NULL) {
		DRM_ERROR("entry already on freelist.\n");
		return;
	}

	prev = &dev_priv->head;
	next = prev->next;
	prev->next = entry;
	next->prev = entry;
	entry->prev = prev;
	entry->next = next;
}

/*
 * Command DMA
 */
D
Dave Airlie 已提交
295
static int savage_dma_init(drm_savage_private_t * dev_priv)
D
Dave Airlie 已提交
296 297 298 299
{
	unsigned int i;

	dev_priv->nr_dma_pages = dev_priv->cmd_dma->size /
D
Dave Airlie 已提交
300
	    (SAVAGE_DMA_PAGE_SIZE * 4);
301 302
	dev_priv->dma_pages = kmalloc(sizeof(drm_savage_dma_page_t) *
				      dev_priv->nr_dma_pages, GFP_KERNEL);
D
Dave Airlie 已提交
303
	if (dev_priv->dma_pages == NULL)
E
Eric Anholt 已提交
304
		return -ENOMEM;
D
Dave Airlie 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318

	for (i = 0; i < dev_priv->nr_dma_pages; ++i) {
		SET_AGE(&dev_priv->dma_pages[i].age, 0, 0);
		dev_priv->dma_pages[i].used = 0;
		dev_priv->dma_pages[i].flushed = 0;
	}
	SET_AGE(&dev_priv->last_dma_age, 0, 0);

	dev_priv->first_dma_page = 0;
	dev_priv->current_dma_page = 0;

	return 0;
}

D
Dave Airlie 已提交
319
void savage_dma_reset(drm_savage_private_t * dev_priv)
D
Dave Airlie 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333
{
	uint16_t event;
	unsigned int wrap, i;
	event = savage_bci_emit_event(dev_priv, 0);
	wrap = dev_priv->event_wrap;
	for (i = 0; i < dev_priv->nr_dma_pages; ++i) {
		SET_AGE(&dev_priv->dma_pages[i].age, event, wrap);
		dev_priv->dma_pages[i].used = 0;
		dev_priv->dma_pages[i].flushed = 0;
	}
	SET_AGE(&dev_priv->last_dma_age, event, wrap);
	dev_priv->first_dma_page = dev_priv->current_dma_page = 0;
}

D
Dave Airlie 已提交
334
void savage_dma_wait(drm_savage_private_t * dev_priv, unsigned int page)
D
Dave Airlie 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
{
	uint16_t event;
	unsigned int wrap;

	/* Faked DMA buffer pages don't age. */
	if (dev_priv->cmd_dma == &dev_priv->fake_dma)
		return;

	UPDATE_EVENT_COUNTER();
	if (dev_priv->status_ptr)
		event = dev_priv->status_ptr[1] & 0xffff;
	else
		event = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff;
	wrap = dev_priv->event_wrap;
	if (event > dev_priv->event_counter)
D
Dave Airlie 已提交
350
		wrap--;		/* hardware hasn't passed the last wrap yet */
D
Dave Airlie 已提交
351 352 353 354 355 356 357 358 359 360 361

	if (dev_priv->dma_pages[page].age.wrap > wrap ||
	    (dev_priv->dma_pages[page].age.wrap == wrap &&
	     dev_priv->dma_pages[page].age.event > event)) {
		if (dev_priv->wait_evnt(dev_priv,
					dev_priv->dma_pages[page].age.event)
		    < 0)
			DRM_ERROR("wait_evnt failed!\n");
	}
}

D
Dave Airlie 已提交
362
uint32_t *savage_dma_alloc(drm_savage_private_t * dev_priv, unsigned int n)
D
Dave Airlie 已提交
363 364 365
{
	unsigned int cur = dev_priv->current_dma_page;
	unsigned int rest = SAVAGE_DMA_PAGE_SIZE -
D
Dave Airlie 已提交
366 367 368
	    dev_priv->dma_pages[cur].used;
	unsigned int nr_pages = (n - rest + SAVAGE_DMA_PAGE_SIZE - 1) /
	    SAVAGE_DMA_PAGE_SIZE;
D
Dave Airlie 已提交
369 370 371 372 373 374 375
	uint32_t *dma_ptr;
	unsigned int i;

	DRM_DEBUG("cur=%u, cur->used=%u, n=%u, rest=%u, nr_pages=%u\n",
		  cur, dev_priv->dma_pages[cur].used, n, rest, nr_pages);

	if (cur + nr_pages < dev_priv->nr_dma_pages) {
D
Dave Airlie 已提交
376 377
		dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle +
		    cur * SAVAGE_DMA_PAGE_SIZE + dev_priv->dma_pages[cur].used;
D
Dave Airlie 已提交
378 379 380 381 382 383 384
		if (n < rest)
			rest = n;
		dev_priv->dma_pages[cur].used += rest;
		n -= rest;
		cur++;
	} else {
		dev_priv->dma_flush(dev_priv);
D
Dave Airlie 已提交
385 386
		nr_pages =
		    (n + SAVAGE_DMA_PAGE_SIZE - 1) / SAVAGE_DMA_PAGE_SIZE;
D
Dave Airlie 已提交
387 388 389 390 391
		for (i = cur; i < dev_priv->nr_dma_pages; ++i) {
			dev_priv->dma_pages[i].age = dev_priv->last_dma_age;
			dev_priv->dma_pages[i].used = 0;
			dev_priv->dma_pages[i].flushed = 0;
		}
D
Dave Airlie 已提交
392
		dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle;
D
Dave Airlie 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
		dev_priv->first_dma_page = cur = 0;
	}
	for (i = cur; nr_pages > 0; ++i, --nr_pages) {
#if SAVAGE_DMA_DEBUG
		if (dev_priv->dma_pages[i].used) {
			DRM_ERROR("unflushed page %u: used=%u\n",
				  i, dev_priv->dma_pages[i].used);
		}
#endif
		if (n > SAVAGE_DMA_PAGE_SIZE)
			dev_priv->dma_pages[i].used = SAVAGE_DMA_PAGE_SIZE;
		else
			dev_priv->dma_pages[i].used = n;
		n -= SAVAGE_DMA_PAGE_SIZE;
	}
	dev_priv->current_dma_page = --i;

	DRM_DEBUG("cur=%u, cur->used=%u, n=%u\n",
		  i, dev_priv->dma_pages[i].used, n);

	savage_dma_wait(dev_priv, dev_priv->current_dma_page);

	return dma_ptr;
}

D
Dave Airlie 已提交
418
static void savage_dma_flush(drm_savage_private_t * dev_priv)
D
Dave Airlie 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
{
	unsigned int first = dev_priv->first_dma_page;
	unsigned int cur = dev_priv->current_dma_page;
	uint16_t event;
	unsigned int wrap, pad, align, len, i;
	unsigned long phys_addr;
	BCI_LOCALS;

	if (first == cur &&
	    dev_priv->dma_pages[cur].used == dev_priv->dma_pages[cur].flushed)
		return;

	/* pad length to multiples of 2 entries
	 * align start of next DMA block to multiles of 8 entries */
	pad = -dev_priv->dma_pages[cur].used & 1;
	align = -(dev_priv->dma_pages[cur].used + pad) & 7;

	DRM_DEBUG("first=%u, cur=%u, first->flushed=%u, cur->used=%u, "
		  "pad=%u, align=%u\n",
		  first, cur, dev_priv->dma_pages[first].flushed,
		  dev_priv->dma_pages[cur].used, pad, align);

	/* pad with noops */
	if (pad) {
D
Dave Airlie 已提交
443 444
		uint32_t *dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle +
		    cur * SAVAGE_DMA_PAGE_SIZE + dev_priv->dma_pages[cur].used;
D
Dave Airlie 已提交
445
		dev_priv->dma_pages[cur].used += pad;
D
Dave Airlie 已提交
446
		while (pad != 0) {
D
Dave Airlie 已提交
447 448 449 450 451 452 453 454 455
			*dma_ptr++ = BCI_CMD_WAIT;
			pad--;
		}
	}

	DRM_MEMORYBARRIER();

	/* do flush ... */
	phys_addr = dev_priv->cmd_dma->offset +
D
Dave Airlie 已提交
456 457
	    (first * SAVAGE_DMA_PAGE_SIZE +
	     dev_priv->dma_pages[first].flushed) * 4;
D
Dave Airlie 已提交
458
	len = (cur - first) * SAVAGE_DMA_PAGE_SIZE +
D
Dave Airlie 已提交
459
	    dev_priv->dma_pages[cur].used - dev_priv->dma_pages[first].flushed;
D
Dave Airlie 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

	DRM_DEBUG("phys_addr=%lx, len=%u\n",
		  phys_addr | dev_priv->dma_type, len);

	BEGIN_BCI(3);
	BCI_SET_REGISTERS(SAVAGE_DMABUFADDR, 1);
	BCI_WRITE(phys_addr | dev_priv->dma_type);
	BCI_DMA(len);

	/* fix alignment of the start of the next block */
	dev_priv->dma_pages[cur].used += align;

	/* age DMA pages */
	event = savage_bci_emit_event(dev_priv, 0);
	wrap = dev_priv->event_wrap;
	for (i = first; i < cur; ++i) {
		SET_AGE(&dev_priv->dma_pages[i].age, event, wrap);
		dev_priv->dma_pages[i].used = 0;
		dev_priv->dma_pages[i].flushed = 0;
	}
	/* age the current page only when it's full */
	if (dev_priv->dma_pages[cur].used == SAVAGE_DMA_PAGE_SIZE) {
		SET_AGE(&dev_priv->dma_pages[cur].age, event, wrap);
		dev_priv->dma_pages[cur].used = 0;
		dev_priv->dma_pages[cur].flushed = 0;
		/* advance to next page */
		cur++;
		if (cur == dev_priv->nr_dma_pages)
			cur = 0;
		dev_priv->first_dma_page = dev_priv->current_dma_page = cur;
	} else {
		dev_priv->first_dma_page = cur;
		dev_priv->dma_pages[cur].flushed = dev_priv->dma_pages[i].used;
	}
	SET_AGE(&dev_priv->last_dma_age, event, wrap);

	DRM_DEBUG("first=cur=%u, cur->used=%u, cur->flushed=%u\n", cur,
		  dev_priv->dma_pages[cur].used,
		  dev_priv->dma_pages[cur].flushed);
}

D
Dave Airlie 已提交
501
static void savage_fake_dma_flush(drm_savage_private_t * dev_priv)
D
Dave Airlie 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
{
	unsigned int i, j;
	BCI_LOCALS;

	if (dev_priv->first_dma_page == dev_priv->current_dma_page &&
	    dev_priv->dma_pages[dev_priv->current_dma_page].used == 0)
		return;

	DRM_DEBUG("first=%u, cur=%u, cur->used=%u\n",
		  dev_priv->first_dma_page, dev_priv->current_dma_page,
		  dev_priv->dma_pages[dev_priv->current_dma_page].used);

	for (i = dev_priv->first_dma_page;
	     i <= dev_priv->current_dma_page && dev_priv->dma_pages[i].used;
	     ++i) {
D
Dave Airlie 已提交
517 518
		uint32_t *dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle +
		    i * SAVAGE_DMA_PAGE_SIZE;
D
Dave Airlie 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
#if SAVAGE_DMA_DEBUG
		/* Sanity check: all pages except the last one must be full. */
		if (i < dev_priv->current_dma_page &&
		    dev_priv->dma_pages[i].used != SAVAGE_DMA_PAGE_SIZE) {
			DRM_ERROR("partial DMA page %u: used=%u",
				  i, dev_priv->dma_pages[i].used);
		}
#endif
		BEGIN_BCI(dev_priv->dma_pages[i].used);
		for (j = 0; j < dev_priv->dma_pages[i].used; ++j) {
			BCI_WRITE(dma_ptr[j]);
		}
		dev_priv->dma_pages[i].used = 0;
	}

	/* reset to first page */
	dev_priv->first_dma_page = dev_priv->current_dma_page = 0;
}

538
int savage_driver_load(struct drm_device *dev, unsigned long chipset)
539 540 541
{
	drm_savage_private_t *dev_priv;

J
Julia Lawall 已提交
542
	dev_priv = kzalloc(sizeof(drm_savage_private_t), GFP_KERNEL);
543
	if (dev_priv == NULL)
E
Eric Anholt 已提交
544
		return -ENOMEM;
545 546 547 548 549 550 551 552 553

	dev->dev_private = (void *)dev_priv;

	dev_priv->chipset = (enum savage_family)chipset;

	return 0;
}


D
Dave Airlie 已提交
554
/*
555
 * Initialize mappings. On Savage4 and SavageIX the alignment
D
Dave Airlie 已提交
556
 * and size of the aperture is not suitable for automatic MTRR setup
557 558
 * in drm_addmap. Therefore we add them manually before the maps are
 * initialized, and tear them down on last close.
D
Dave Airlie 已提交
559
 */
560
int savage_driver_firstopen(struct drm_device *dev)
D
Dave Airlie 已提交
561
{
562
	drm_savage_private_t *dev_priv = dev->dev_private;
D
Dave Airlie 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575
	unsigned long mmio_base, fb_base, fb_size, aperture_base;
	/* fb_rsrc and aper_rsrc aren't really used currently, but still exist
	 * in case we decide we need information on the BAR for BSD in the
	 * future.
	 */
	unsigned int fb_rsrc, aper_rsrc;
	int ret = 0;

	dev_priv->mtrr[0].handle = -1;
	dev_priv->mtrr[1].handle = -1;
	dev_priv->mtrr[2].handle = -1;
	if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
		fb_rsrc = 0;
576
		fb_base = pci_resource_start(dev->pdev, 0);
D
Dave Airlie 已提交
577 578 579 580 581
		fb_size = SAVAGE_FB_SIZE_S3;
		mmio_base = fb_base + SAVAGE_FB_SIZE_S3;
		aper_rsrc = 0;
		aperture_base = fb_base + SAVAGE_APERTURE_OFFSET;
		/* this should always be true */
582
		if (pci_resource_len(dev->pdev, 0) == 0x08000000) {
D
Dave Airlie 已提交
583 584 585 586
			/* Don't make MMIO write-cobining! We need 3
			 * MTRRs. */
			dev_priv->mtrr[0].base = fb_base;
			dev_priv->mtrr[0].size = 0x01000000;
D
Dave Airlie 已提交
587
			dev_priv->mtrr[0].handle =
588 589
			    drm_mtrr_add(dev_priv->mtrr[0].base,
				         dev_priv->mtrr[0].size, DRM_MTRR_WC);
D
Dave Airlie 已提交
590
			dev_priv->mtrr[1].base = fb_base + 0x02000000;
D
Dave Airlie 已提交
591
			dev_priv->mtrr[1].size = 0x02000000;
D
Dave Airlie 已提交
592
			dev_priv->mtrr[1].handle =
593 594
			    drm_mtrr_add(dev_priv->mtrr[1].base,
					 dev_priv->mtrr[1].size, DRM_MTRR_WC);
D
Dave Airlie 已提交
595
			dev_priv->mtrr[2].base = fb_base + 0x04000000;
D
Dave Airlie 已提交
596
			dev_priv->mtrr[2].size = 0x04000000;
D
Dave Airlie 已提交
597
			dev_priv->mtrr[2].handle =
598 599
			    drm_mtrr_add(dev_priv->mtrr[2].base,
					 dev_priv->mtrr[2].size, DRM_MTRR_WC);
D
Dave Airlie 已提交
600
		} else {
601
			DRM_ERROR("strange pci_resource_len %08llx\n",
602 603
				  (unsigned long long)
				  pci_resource_len(dev->pdev, 0));
D
Dave Airlie 已提交
604
		}
605 606
	} else if (dev_priv->chipset != S3_SUPERSAVAGE &&
		   dev_priv->chipset != S3_SAVAGE2000) {
607
		mmio_base = pci_resource_start(dev->pdev, 0);
D
Dave Airlie 已提交
608
		fb_rsrc = 1;
609
		fb_base = pci_resource_start(dev->pdev, 1);
D
Dave Airlie 已提交
610 611 612 613
		fb_size = SAVAGE_FB_SIZE_S4;
		aper_rsrc = 1;
		aperture_base = fb_base + SAVAGE_APERTURE_OFFSET;
		/* this should always be true */
614
		if (pci_resource_len(dev->pdev, 1) == 0x08000000) {
D
Dave Airlie 已提交
615 616 617 618
			/* Can use one MTRR to cover both fb and
			 * aperture. */
			dev_priv->mtrr[0].base = fb_base;
			dev_priv->mtrr[0].size = 0x08000000;
D
Dave Airlie 已提交
619
			dev_priv->mtrr[0].handle =
620 621
			    drm_mtrr_add(dev_priv->mtrr[0].base,
					 dev_priv->mtrr[0].size, DRM_MTRR_WC);
D
Dave Airlie 已提交
622
		} else {
623
			DRM_ERROR("strange pci_resource_len %08llx\n",
624 625
				  (unsigned long long)
				  pci_resource_len(dev->pdev, 1));
D
Dave Airlie 已提交
626 627
		}
	} else {
628
		mmio_base = pci_resource_start(dev->pdev, 0);
D
Dave Airlie 已提交
629
		fb_rsrc = 1;
630 631
		fb_base = pci_resource_start(dev->pdev, 1);
		fb_size = pci_resource_len(dev->pdev, 1);
D
Dave Airlie 已提交
632
		aper_rsrc = 2;
633
		aperture_base = pci_resource_start(dev->pdev, 2);
D
Dave Airlie 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
		/* Automatic MTRR setup will do the right thing. */
	}

	ret = drm_addmap(dev, mmio_base, SAVAGE_MMIO_SIZE, _DRM_REGISTERS,
			 _DRM_READ_ONLY, &dev_priv->mmio);
	if (ret)
		return ret;

	ret = drm_addmap(dev, fb_base, fb_size, _DRM_FRAME_BUFFER,
			 _DRM_WRITE_COMBINING, &dev_priv->fb);
	if (ret)
		return ret;

	ret = drm_addmap(dev, aperture_base, SAVAGE_APERTURE_SIZE,
			 _DRM_FRAME_BUFFER, _DRM_WRITE_COMBINING,
			 &dev_priv->aperture);
	return ret;
}

/*
 * Delete MTRRs and free device-private data.
 */
656
void savage_driver_lastclose(struct drm_device *dev)
D
Dave Airlie 已提交
657 658 659 660 661 662
{
	drm_savage_private_t *dev_priv = dev->dev_private;
	int i;

	for (i = 0; i < 3; ++i)
		if (dev_priv->mtrr[i].handle >= 0)
663
			drm_mtrr_del(dev_priv->mtrr[i].handle,
D
Dave Airlie 已提交
664
				 dev_priv->mtrr[i].base,
665
				 dev_priv->mtrr[i].size, DRM_MTRR_WC);
666 667
}

668
int savage_driver_unload(struct drm_device *dev)
669 670
{
	drm_savage_private_t *dev_priv = dev->dev_private;
D
Dave Airlie 已提交
671

672
	kfree(dev_priv);
D
Dave Airlie 已提交
673 674 675 676

	return 0;
}

677
static int savage_do_init_bci(struct drm_device * dev, drm_savage_init_t * init)
D
Dave Airlie 已提交
678 679 680 681 682
{
	drm_savage_private_t *dev_priv = dev->dev_private;

	if (init->fb_bpp != 16 && init->fb_bpp != 32) {
		DRM_ERROR("invalid frame buffer bpp %d!\n", init->fb_bpp);
E
Eric Anholt 已提交
683
		return -EINVAL;
D
Dave Airlie 已提交
684 685 686
	}
	if (init->depth_bpp != 16 && init->depth_bpp != 32) {
		DRM_ERROR("invalid depth buffer bpp %d!\n", init->fb_bpp);
E
Eric Anholt 已提交
687
		return -EINVAL;
D
Dave Airlie 已提交
688 689 690 691
	}
	if (init->dma_type != SAVAGE_DMA_AGP &&
	    init->dma_type != SAVAGE_DMA_PCI) {
		DRM_ERROR("invalid dma memory type %d!\n", init->dma_type);
E
Eric Anholt 已提交
692
		return -EINVAL;
D
Dave Airlie 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	}

	dev_priv->cob_size = init->cob_size;
	dev_priv->bci_threshold_lo = init->bci_threshold_lo;
	dev_priv->bci_threshold_hi = init->bci_threshold_hi;
	dev_priv->dma_type = init->dma_type;

	dev_priv->fb_bpp = init->fb_bpp;
	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;
	dev_priv->depth_bpp = init->depth_bpp;
	dev_priv->depth_offset = init->depth_offset;
	dev_priv->depth_pitch = init->depth_pitch;

	dev_priv->texture_offset = init->texture_offset;
	dev_priv->texture_size = init->texture_size;

712
	dev_priv->sarea = drm_getsarea(dev);
D
Dave Airlie 已提交
713 714 715
	if (!dev_priv->sarea) {
		DRM_ERROR("could not find sarea!\n");
		savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
716
		return -EINVAL;
D
Dave Airlie 已提交
717 718 719 720 721 722
	}
	if (init->status_offset != 0) {
		dev_priv->status = drm_core_findmap(dev, init->status_offset);
		if (!dev_priv->status) {
			DRM_ERROR("could not find shadow status region!\n");
			savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
723
			return -EINVAL;
D
Dave Airlie 已提交
724 725 726 727 728
		}
	} else {
		dev_priv->status = NULL;
	}
	if (dev_priv->dma_type == SAVAGE_DMA_AGP && init->buffers_offset) {
729
		dev->agp_buffer_token = init->buffers_offset;
D
Dave Airlie 已提交
730 731 732 733 734
		dev->agp_buffer_map = drm_core_findmap(dev,
						       init->buffers_offset);
		if (!dev->agp_buffer_map) {
			DRM_ERROR("could not find DMA buffer region!\n");
			savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
735
			return -EINVAL;
D
Dave Airlie 已提交
736 737
		}
		drm_core_ioremap(dev->agp_buffer_map, dev);
738
		if (!dev->agp_buffer_map->handle) {
D
Dave Airlie 已提交
739 740
			DRM_ERROR("failed to ioremap DMA buffer region!\n");
			savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
741
			return -ENOMEM;
D
Dave Airlie 已提交
742 743 744 745
		}
	}
	if (init->agp_textures_offset) {
		dev_priv->agp_textures =
D
Dave Airlie 已提交
746
		    drm_core_findmap(dev, init->agp_textures_offset);
D
Dave Airlie 已提交
747 748 749
		if (!dev_priv->agp_textures) {
			DRM_ERROR("could not find agp texture region!\n");
			savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
750
			return -EINVAL;
D
Dave Airlie 已提交
751 752 753 754 755 756 757 758 759 760
		}
	} else {
		dev_priv->agp_textures = NULL;
	}

	if (init->cmd_dma_offset) {
		if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
			DRM_ERROR("command DMA not supported on "
				  "Savage3D/MX/IX.\n");
			savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
761
			return -EINVAL;
D
Dave Airlie 已提交
762 763 764 765 766
		}
		if (dev->dma && dev->dma->buflist) {
			DRM_ERROR("command and vertex DMA not supported "
				  "at the same time.\n");
			savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
767
			return -EINVAL;
D
Dave Airlie 已提交
768 769 770 771 772
		}
		dev_priv->cmd_dma = drm_core_findmap(dev, init->cmd_dma_offset);
		if (!dev_priv->cmd_dma) {
			DRM_ERROR("could not find command DMA region!\n");
			savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
773
			return -EINVAL;
D
Dave Airlie 已提交
774 775 776 777 778 779
		}
		if (dev_priv->dma_type == SAVAGE_DMA_AGP) {
			if (dev_priv->cmd_dma->type != _DRM_AGP) {
				DRM_ERROR("AGP command DMA region is not a "
					  "_DRM_AGP map!\n");
				savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
780
				return -EINVAL;
D
Dave Airlie 已提交
781 782 783 784 785 786
			}
			drm_core_ioremap(dev_priv->cmd_dma, dev);
			if (!dev_priv->cmd_dma->handle) {
				DRM_ERROR("failed to ioremap command "
					  "DMA region!\n");
				savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
787
				return -ENOMEM;
D
Dave Airlie 已提交
788 789 790 791 792
			}
		} else if (dev_priv->cmd_dma->type != _DRM_CONSISTENT) {
			DRM_ERROR("PCI command DMA region is not a "
				  "_DRM_CONSISTENT map!\n");
			savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
793
			return -EINVAL;
D
Dave Airlie 已提交
794 795 796 797 798 799 800 801 802 803 804
		}
	} else {
		dev_priv->cmd_dma = NULL;
	}

	dev_priv->dma_flush = savage_dma_flush;
	if (!dev_priv->cmd_dma) {
		DRM_DEBUG("falling back to faked command DMA.\n");
		dev_priv->fake_dma.offset = 0;
		dev_priv->fake_dma.size = SAVAGE_FAKE_DMA_SIZE;
		dev_priv->fake_dma.type = _DRM_SHM;
805 806
		dev_priv->fake_dma.handle = kmalloc(SAVAGE_FAKE_DMA_SIZE,
						    GFP_KERNEL);
D
Dave Airlie 已提交
807 808 809
		if (!dev_priv->fake_dma.handle) {
			DRM_ERROR("could not allocate faked DMA buffer!\n");
			savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
810
			return -ENOMEM;
D
Dave Airlie 已提交
811 812 813 814 815 816
		}
		dev_priv->cmd_dma = &dev_priv->fake_dma;
		dev_priv->dma_flush = savage_fake_dma_flush;
	}

	dev_priv->sarea_priv =
D
Dave Airlie 已提交
817 818
	    (drm_savage_sarea_t *) ((uint8_t *) dev_priv->sarea->handle +
				    init->sarea_priv_offset);
D
Dave Airlie 已提交
819 820 821 822 823 824 825 826

	/* setup bitmap descriptors */
	{
		unsigned int color_tile_format;
		unsigned int depth_tile_format;
		unsigned int front_stride, back_stride, depth_stride;
		if (dev_priv->chipset <= S3_SAVAGE4) {
			color_tile_format = dev_priv->fb_bpp == 16 ?
D
Dave Airlie 已提交
827
			    SAVAGE_BD_TILE_16BPP : SAVAGE_BD_TILE_32BPP;
D
Dave Airlie 已提交
828
			depth_tile_format = dev_priv->depth_bpp == 16 ?
D
Dave Airlie 已提交
829
			    SAVAGE_BD_TILE_16BPP : SAVAGE_BD_TILE_32BPP;
D
Dave Airlie 已提交
830 831 832 833
		} else {
			color_tile_format = SAVAGE_BD_TILE_DEST;
			depth_tile_format = SAVAGE_BD_TILE_DEST;
		}
D
Dave Airlie 已提交
834 835 836 837
		front_stride = dev_priv->front_pitch / (dev_priv->fb_bpp / 8);
		back_stride = dev_priv->back_pitch / (dev_priv->fb_bpp / 8);
		depth_stride =
		    dev_priv->depth_pitch / (dev_priv->depth_bpp / 8);
D
Dave Airlie 已提交
838 839

		dev_priv->front_bd = front_stride | SAVAGE_BD_BW_DISABLE |
D
Dave Airlie 已提交
840 841
		    (dev_priv->fb_bpp << SAVAGE_BD_BPP_SHIFT) |
		    (color_tile_format << SAVAGE_BD_TILE_SHIFT);
D
Dave Airlie 已提交
842

D
Dave Airlie 已提交
843 844 845
		dev_priv->back_bd = back_stride | SAVAGE_BD_BW_DISABLE |
		    (dev_priv->fb_bpp << SAVAGE_BD_BPP_SHIFT) |
		    (color_tile_format << SAVAGE_BD_TILE_SHIFT);
D
Dave Airlie 已提交
846 847

		dev_priv->depth_bd = depth_stride | SAVAGE_BD_BW_DISABLE |
D
Dave Airlie 已提交
848 849
		    (dev_priv->depth_bpp << SAVAGE_BD_BPP_SHIFT) |
		    (depth_tile_format << SAVAGE_BD_TILE_SHIFT);
D
Dave Airlie 已提交
850 851 852 853 854 855
	}

	/* setup status and bci ptr */
	dev_priv->event_counter = 0;
	dev_priv->event_wrap = 0;
	dev_priv->bci_ptr = (volatile uint32_t *)
D
Dave Airlie 已提交
856
	    ((uint8_t *) dev_priv->mmio->handle + SAVAGE_BCI_OFFSET);
D
Dave Airlie 已提交
857 858 859 860 861 862 863
	if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
		dev_priv->status_used_mask = SAVAGE_FIFO_USED_MASK_S3D;
	} else {
		dev_priv->status_used_mask = SAVAGE_FIFO_USED_MASK_S4;
	}
	if (dev_priv->status != NULL) {
		dev_priv->status_ptr =
D
Dave Airlie 已提交
864
		    (volatile uint32_t *)dev_priv->status->handle;
D
Dave Airlie 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
		dev_priv->wait_fifo = savage_bci_wait_fifo_shadow;
		dev_priv->wait_evnt = savage_bci_wait_event_shadow;
		dev_priv->status_ptr[1023] = dev_priv->event_counter;
	} else {
		dev_priv->status_ptr = NULL;
		if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
			dev_priv->wait_fifo = savage_bci_wait_fifo_s3d;
		} else {
			dev_priv->wait_fifo = savage_bci_wait_fifo_s4;
		}
		dev_priv->wait_evnt = savage_bci_wait_event_reg;
	}

	/* cliprect functions */
	if (S3_SAVAGE3D_SERIES(dev_priv->chipset))
		dev_priv->emit_clip_rect = savage_emit_clip_rect_s3d;
	else
		dev_priv->emit_clip_rect = savage_emit_clip_rect_s4;

	if (savage_freelist_init(dev) < 0) {
		DRM_ERROR("could not initialize freelist\n");
		savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
887
		return -ENOMEM;
D
Dave Airlie 已提交
888 889
	}

D
Dave Airlie 已提交
890
	if (savage_dma_init(dev_priv) < 0) {
D
Dave Airlie 已提交
891 892
		DRM_ERROR("could not initialize command DMA\n");
		savage_do_cleanup_bci(dev);
E
Eric Anholt 已提交
893
		return -ENOMEM;
D
Dave Airlie 已提交
894 895 896 897 898
	}

	return 0;
}

899
static int savage_do_cleanup_bci(struct drm_device * dev)
D
Dave Airlie 已提交
900 901 902 903
{
	drm_savage_private_t *dev_priv = dev->dev_private;

	if (dev_priv->cmd_dma == &dev_priv->fake_dma) {
904
		kfree(dev_priv->fake_dma.handle);
D
Dave Airlie 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918
	} else if (dev_priv->cmd_dma && dev_priv->cmd_dma->handle &&
		   dev_priv->cmd_dma->type == _DRM_AGP &&
		   dev_priv->dma_type == SAVAGE_DMA_AGP)
		drm_core_ioremapfree(dev_priv->cmd_dma, dev);

	if (dev_priv->dma_type == SAVAGE_DMA_AGP &&
	    dev->agp_buffer_map && dev->agp_buffer_map->handle) {
		drm_core_ioremapfree(dev->agp_buffer_map, dev);
		/* make sure the next instance (which may be running
		 * in PCI mode) doesn't try to use an old
		 * agp_buffer_map. */
		dev->agp_buffer_map = NULL;
	}

919
	kfree(dev_priv->dma_pages);
D
Dave Airlie 已提交
920 921 922 923

	return 0;
}

924
static int savage_bci_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
D
Dave Airlie 已提交
925
{
926
	drm_savage_init_t *init = data;
D
Dave Airlie 已提交
927

928
	LOCK_TEST_WITH_RETURN(dev, file_priv);
D
Dave Airlie 已提交
929

930
	switch (init->func) {
D
Dave Airlie 已提交
931
	case SAVAGE_INIT_BCI:
932
		return savage_do_init_bci(dev, init);
D
Dave Airlie 已提交
933 934 935 936
	case SAVAGE_CLEANUP_BCI:
		return savage_do_cleanup_bci(dev);
	}

E
Eric Anholt 已提交
937
	return -EINVAL;
D
Dave Airlie 已提交
938 939
}

940
static int savage_bci_event_emit(struct drm_device *dev, void *data, struct drm_file *file_priv)
D
Dave Airlie 已提交
941 942
{
	drm_savage_private_t *dev_priv = dev->dev_private;
943
	drm_savage_event_emit_t *event = data;
D
Dave Airlie 已提交
944 945 946

	DRM_DEBUG("\n");

947
	LOCK_TEST_WITH_RETURN(dev, file_priv);
D
Dave Airlie 已提交
948

949 950
	event->count = savage_bci_emit_event(dev_priv, event->flags);
	event->count |= dev_priv->event_wrap << 16;
D
Dave Airlie 已提交
951 952 953 954

	return 0;
}

955
static int savage_bci_event_wait(struct drm_device *dev, void *data, struct drm_file *file_priv)
D
Dave Airlie 已提交
956 957
{
	drm_savage_private_t *dev_priv = dev->dev_private;
958
	drm_savage_event_wait_t *event = data;
D
Dave Airlie 已提交
959 960 961 962 963 964 965 966 967 968 969 970
	unsigned int event_e, hw_e;
	unsigned int event_w, hw_w;

	DRM_DEBUG("\n");

	UPDATE_EVENT_COUNTER();
	if (dev_priv->status_ptr)
		hw_e = dev_priv->status_ptr[1] & 0xffff;
	else
		hw_e = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff;
	hw_w = dev_priv->event_wrap;
	if (hw_e > dev_priv->event_counter)
D
Dave Airlie 已提交
971
		hw_w--;		/* hardware hasn't passed the last wrap yet */
D
Dave Airlie 已提交
972

973 974
	event_e = event->count & 0xffff;
	event_w = event->count >> 16;
D
Dave Airlie 已提交
975 976 977 978 979

	/* Don't need to wait if
	 * - event counter wrapped since the event was emitted or
	 * - the hardware has advanced up to or over the event to wait for.
	 */
D
Dave Airlie 已提交
980
	if (event_w < hw_w || (event_w == hw_w && event_e <= hw_e))
D
Dave Airlie 已提交
981 982 983 984 985 986 987 988 989
		return 0;
	else
		return dev_priv->wait_evnt(dev_priv, event_e);
}

/*
 * DMA buffer management
 */

990 991 992
static int savage_bci_get_buffers(struct drm_device *dev,
				  struct drm_file *file_priv,
				  struct drm_dma *d)
D
Dave Airlie 已提交
993
{
D
Dave Airlie 已提交
994
	struct drm_buf *buf;
D
Dave Airlie 已提交
995 996 997 998 999
	int i;

	for (i = d->granted_count; i < d->request_count; i++) {
		buf = savage_freelist_get(dev);
		if (!buf)
E
Eric Anholt 已提交
1000
			return -EAGAIN;
D
Dave Airlie 已提交
1001

1002
		buf->file_priv = file_priv;
D
Dave Airlie 已提交
1003 1004 1005

		if (DRM_COPY_TO_USER(&d->request_indices[i],
				     &buf->idx, sizeof(buf->idx)))
E
Eric Anholt 已提交
1006
			return -EFAULT;
D
Dave Airlie 已提交
1007 1008
		if (DRM_COPY_TO_USER(&d->request_sizes[i],
				     &buf->total, sizeof(buf->total)))
E
Eric Anholt 已提交
1009
			return -EFAULT;
D
Dave Airlie 已提交
1010 1011 1012 1013 1014 1015

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

1016
int savage_bci_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
D
Dave Airlie 已提交
1017
{
1018
	struct drm_device_dma *dma = dev->dma;
1019
	struct drm_dma *d = data;
D
Dave Airlie 已提交
1020 1021
	int ret = 0;

1022
	LOCK_TEST_WITH_RETURN(dev, file_priv);
D
Dave Airlie 已提交
1023 1024 1025

	/* Please don't send us buffers.
	 */
1026
	if (d->send_count != 0) {
D
Dave Airlie 已提交
1027
		DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
1028
			  DRM_CURRENTPID, d->send_count);
E
Eric Anholt 已提交
1029
		return -EINVAL;
D
Dave Airlie 已提交
1030 1031 1032 1033
	}

	/* We'll send you buffers.
	 */
1034
	if (d->request_count < 0 || d->request_count > dma->buf_count) {
D
Dave Airlie 已提交
1035
		DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
1036
			  DRM_CURRENTPID, d->request_count, dma->buf_count);
E
Eric Anholt 已提交
1037
		return -EINVAL;
D
Dave Airlie 已提交
1038 1039
	}

1040
	d->granted_count = 0;
D
Dave Airlie 已提交
1041

1042 1043
	if (d->request_count) {
		ret = savage_bci_get_buffers(dev, file_priv, d);
D
Dave Airlie 已提交
1044 1045 1046 1047 1048
	}

	return ret;
}

1049
void savage_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv)
D
Dave Airlie 已提交
1050
{
1051
	struct drm_device_dma *dma = dev->dma;
D
Dave Airlie 已提交
1052
	drm_savage_private_t *dev_priv = dev->dev_private;
1053
	int release_idlelock = 0;
D
Dave Airlie 已提交
1054 1055 1056 1057 1058 1059 1060 1061 1062
	int i;

	if (!dma)
		return;
	if (!dev_priv)
		return;
	if (!dma->buflist)
		return;

1063 1064 1065 1066
	if (file_priv->master && file_priv->master->lock.hw_lock) {
		drm_idlelock_take(&file_priv->master->lock);
		release_idlelock = 1;
	}
D
Dave Airlie 已提交
1067 1068

	for (i = 0; i < dma->buf_count; i++) {
D
Dave Airlie 已提交
1069
		struct drm_buf *buf = dma->buflist[i];
D
Dave Airlie 已提交
1070 1071
		drm_savage_buf_priv_t *buf_priv = buf->dev_private;

1072
		if (buf->file_priv == file_priv && buf_priv &&
D
Dave Airlie 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081
		    buf_priv->next == NULL && buf_priv->prev == NULL) {
			uint16_t event;
			DRM_DEBUG("reclaimed from client\n");
			event = savage_bci_emit_event(dev_priv, SAVAGE_WAIT_3D);
			SET_AGE(&buf_priv->age, event, dev_priv->event_wrap);
			savage_freelist_put(dev, buf);
		}
	}

1082 1083
	if (release_idlelock)
		drm_idlelock_release(&file_priv->master->lock);
D
Dave Airlie 已提交
1084 1085
}

1086
struct drm_ioctl_desc savage_ioctls[] = {
1087 1088 1089 1090
	DRM_IOCTL_DEF_DRV(SAVAGE_BCI_INIT, savage_bci_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(SAVAGE_BCI_CMDBUF, savage_bci_cmdbuf, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(SAVAGE_BCI_EVENT_EMIT, savage_bci_event_emit, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(SAVAGE_BCI_EVENT_WAIT, savage_bci_event_wait, DRM_AUTH),
D
Dave Airlie 已提交
1091 1092 1093
};

int savage_max_ioctl = DRM_ARRAY_SIZE(savage_ioctls);