summaryrefslogtreecommitdiff
AgeCommit message (Expand)Author
2003-11-12Fix a locking nit, and add asserts in some things that should be calledEric Anholt
2003-11-06Return EBUSY when attempting to addmap a DRM_SHM area with a lock in it ifEric Anholt
2003-11-06Remove unused variable.Eric Anholt
2003-11-05Changes to DRM(irq_install)...... wrap dev->dma usage with __HAVE_DMA inAlan Hourihane
2003-11-05- Tie the DRM to a specific device: setunique no longer succeeds when givenEric Anholt
2003-11-05Use int64_t instead of s64 -- fixes FreeBSD compile, works on linux.Eric Anholt
2003-11-05Args for the BSD DRM_PUT_USER_UNCHECKED were swapped.Eric Anholt
2003-11-05__linux__ is spelled with a lowercase 'l'Eric Anholt
2003-11-05Repo-copy linux/drm/kernel/drm.h to shared/drm/kernel/drm.h and use it onEric Anholt
2003-11-05Remove buf_alloc which is unused since the locking commit.Eric Anholt
2003-11-04build fixMichel Daenzer
2003-11-04Memory layout transition:Michel Daenzer
2003-11-03Add i852/i855 PCI ID. Also fix whitespace in the other ID definitions.Eric Anholt
2003-10-27Add a DRM_PUT_USER_UNCHECKED, which will be used by an upcoming radeonEric Anholt
2003-10-24Don't try to use dev->dma_lock unless dma is initialized (dev->dma != NULL)Eric Anholt
2003-10-24Switch pci enumeration call to work on both 2.4 and 2.6 pci_for_each_dev isJon Smirl
2003-10-24Move the REALLY_HAVE_AGP endif above the mtrr functions. Broke tdfx module.Eric Anholt
2003-10-23Move to "old-style" probing as documented in linux/Documentation/pci.txt.Eric Anholt
2003-10-23Missed the sis.h header in Makefile.linux in the move to shared/.Eric Anholt
2003-10-23The SiS300 pci id also covers the 305, so make the name reflect that.Eric Anholt
2003-10-23- Introduce a new ioctl, DRM_IOCTL_SET_VERSION. This ioctl allows theEric Anholt
2003-10-22- Add DRM_GET_PRIV_WITH_RETURN macro. This can be used in shared code toEric Anholt
2003-10-22Fix warning about static DRM(bufs_info) defined but not used in theEric Anholt
2003-10-20Fix the possibility of sleeping with locks held in sysctls by copying theEric Anholt
2003-10-20Clean up BSD MTRR handling. The NetBSD code is untested, but it's my bestEric Anholt
2003-10-19- SMPng lock the DRM. This is only partial -- there are a few code pathsEric Anholt
2003-10-19Clean up extra zeroing of dev->dma, and use calloc to take advantage ofEric Anholt
2003-10-19Fix probing on 2.5+ kernels, which require that drivers have .id_table set.Eric Anholt
2003-10-17- Move IRQ functions from drm_dma.h to new drm_irq.h and disentangle themEric Anholt
2003-10-17- Converted Linux drivers to initialize DRM instances based on PCI IDs, notEric Anholt
2003-10-16Introduce COMMIT_RING() as in radeon DRM, stop using error prone writebackMichel Daenzer
2003-10-16Try that again. It's a long.Eric Anholt
2003-10-16Debug printf format fix.Eric Anholt
2003-10-03Some code cleanups done while working on locking. Reduces always-trueEric Anholt
2003-10-03Stylistic preparation for SMPng locking work: DRM_LOCK/DRM_UNLOCK have sideEric Anholt
2003-10-02Add an MIT-style copyright, assigned to myself, to these files. I thinkEric Anholt
2003-10-02Axe more old gamma DMA infrastructure.Eric Anholt
2003-10-02Mostly whitespace cleanups and style(9) fixes focused on "if(" -> "if ("Eric Anholt
2003-10-02Wrap sys/endian.h usage with __FreeBSD_version >= 480000.Eric Anholt
2003-10-02Allow the DRM to attach to a "drmsub" device. This will be provided by theEric Anholt
2003-10-02MTRR issue with SMP and -stable seems to be resolved. Re-enable MTRRs onEric Anholt
2003-09-30file via_drm.h was initially added on branch cle266-0-0-1-branch.Jose Fonseca
2003-09-30file via.h was initially added on branch cle266-0-0-1-branch.Jose Fonseca
2003-09-30file via_drv.c was initially added on branch cle266-0-0-1-branch.Jose Fonseca
2003-09-30file via_drv.h was initially added on branch cle266-0-0-1-branch.Jose Fonseca
2003-09-30file via_ds.c was initially added on branch cle266-0-0-1-branch.Jose Fonseca
2003-09-30file via_ds.h was initially added on branch cle266-0-0-1-branch.Jose Fonseca
2003-09-30file via_map.c was initially added on branch cle266-0-0-1-branch.Jose Fonseca
2003-09-30file via_mm.c was initially added on branch cle266-0-0-1-branch.Jose Fonseca
2003-09-30file via_mm.h was initially added on branch cle266-0-0-1-branch.Jose Fonseca
id='n381' href='#n381'>381 382 383 384 385 386 387 388 389 390 391 392 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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 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 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
/*
 * Copyright 2008 Jerome Glisse.
 * 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.
 *
 * Authors:
 *    Jerome Glisse <glisse@freedesktop.org>
 */
#include "drmP.h"
#include "radeon_drm.h"
#include "radeon_drv.h"
#include "r300_reg.h"

int radeon_cs2_ioctl(struct drm_device *dev, void *data, struct drm_file *fpriv)
{
	struct drm_radeon_cs_parser parser;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct drm_radeon_cs2 *cs = data;
	uint32_t cs_id;
	struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
	uint64_t *chunk_array;
	uint64_t *chunk_array_ptr;
	uint32_t card_offset;
	long size;
	int r, i;
	RING_LOCALS;

	/* set command stream id to 0 which is fake id */
	cs_id = 0;
	cs->cs_id = cs_id;

	if (dev_priv == NULL) {
		DRM_ERROR("called with no initialization\n");
		return -EINVAL;
	}
	if (!cs->num_chunks) {
		return 0;
	}


	chunk_array = drm_calloc(cs->num_chunks, sizeof(uint64_t), DRM_MEM_DRIVER);
	if (!chunk_array) {
		return -ENOMEM;
	}

	chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);

	if (DRM_COPY_FROM_USER(chunk_array, chunk_array_ptr, sizeof(uint64_t)*cs->num_chunks)) {
		r = -EFAULT;
		goto out;
	}

	parser.dev = dev;
	parser.file_priv = fpriv;
	parser.reloc_index = -1;
	parser.ib_index = -1;
	parser.num_chunks = cs->num_chunks;
	/* copy out the chunk headers */
	parser.chunks = drm_calloc(parser.num_chunks, sizeof(struct drm_radeon_kernel_chunk), DRM_MEM_DRIVER);
	if (!parser.chunks) {
		return -ENOMEM;
	}

	for (i = 0; i < parser.num_chunks; i++) {
		struct drm_radeon_cs_chunk user_chunk;

		chunk_ptr = (void __user *)(unsigned long)chunk_array[i];

		if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr, sizeof(struct drm_radeon_cs_chunk))){
			r = -EFAULT;
			goto out;
		}
		parser.chunks[i].chunk_id = user_chunk.chunk_id;

		if (parser.chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS)
			parser.reloc_index = i;

		if (parser.chunks[i].chunk_id == RADEON_CHUNK_ID_IB)
			parser.ib_index = i;

		if (parser.chunks[i].chunk_id == RADEON_CHUNK_ID_OLD) {
			parser.ib_index = i;
			parser.reloc_index = -1;
		}

		parser.chunks[i].length_dw = user_chunk.length_dw;
		parser.chunks[i].chunk_data = (uint32_t *)(unsigned long)user_chunk.chunk_data;

		parser.chunks[i].kdata = NULL;
		size = parser.chunks[i].length_dw * sizeof(uint32_t);

		switch(parser.chunks[i].chunk_id) {
		case RADEON_CHUNK_ID_IB:
		case RADEON_CHUNK_ID_OLD:
			if (size == 0) {
				r = -EINVAL;
				goto out;
			}
		case RADEON_CHUNK_ID_RELOCS:
			if (size) {
				parser.chunks[i].kdata = drm_alloc(size, DRM_MEM_DRIVER);
				if (!parser.chunks[i].kdata) { 
					r = -ENOMEM;
					goto out;
				}
				
				if (DRM_COPY_FROM_USER(parser.chunks[i].kdata, parser.chunks[i].chunk_data, size)) {
					r = -EFAULT;
					goto out;
				}
			} else
				parser.chunks[i].kdata = NULL;
			break;
		default:
			break;
		}
		DRM_DEBUG("chunk %d %d %d %p\n", i, parser.chunks[i].chunk_id, parser.chunks[i].length_dw,
			  parser.chunks[i].chunk_data);
	}


	if (parser.chunks[parser.ib_index].length_dw > (16 * 1024)) {
		DRM_ERROR("cs->dwords too big: %d\n", parser.chunks[parser.ib_index].length_dw);
		r = -EINVAL;
		goto out;
	}

	/* get ib */
	r = dev_priv->cs.ib_get(&parser, &card_offset);
	if (r) {
		DRM_ERROR("ib_get failed\n");
		goto out;
	}

	/* now parse command stream */
	r = dev_priv->cs.parse(&parser);
	if (r) {
		goto out;
	}

	BEGIN_RING(4);
	OUT_RING(CP_PACKET0(RADEON_CP_IB_BASE, 1));
	OUT_RING(card_offset);
	OUT_RING(parser.chunks[parser.ib_index].length_dw);
	OUT_RING(CP_PACKET2());
	ADVANCE_RING();

	/* emit cs id sequence */
	dev_priv->cs.id_emit(dev, &cs_id);
	COMMIT_RING();

	cs->cs_id = cs_id;
		
out:
	dev_priv->cs.ib_free(&parser);

	for (i = 0; i < parser.num_chunks; i++) {
		if (parser.chunks[i].kdata)
			drm_free(parser.chunks[i].kdata, parser.chunks[i].length_dw * sizeof(uint32_t), DRM_MEM_DRIVER);
	}

	drm_free(parser.chunks, sizeof(struct drm_radeon_kernel_chunk)*parser.num_chunks, DRM_MEM_DRIVER);
	drm_free(chunk_array, sizeof(uint64_t)*parser.num_chunks, DRM_MEM_DRIVER);

	return r;
}

int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *fpriv)
{
	struct drm_radeon_cs_parser parser;
	struct drm_radeon_private *dev_priv = dev->dev_private;
	struct drm_radeon_cs *cs = data;
	uint32_t *packets = NULL;
	uint32_t cs_id;
	uint32_t card_offset;
	long size;
	int r;
	struct drm_radeon_kernel_chunk chunk_fake[1];
	RING_LOCALS;

	/* set command stream id to 0 which is fake id */
	cs_id = 0;
	cs->cs_id = cs_id;

	if (dev_priv == NULL) {
		DRM_ERROR("called with no initialization\n");
		return -EINVAL;
	}
	if (!cs->dwords) {
		return 0;
	}
	/* limit cs to 64K ib */
	if (cs->dwords > (16 * 1024)) {
		return -EINVAL;
	}
	/* copy cs from userspace maybe we should copy into ib to save
	 * one copy but ib will be mapped wc so not good for cmd checking
	 * somethings worth testing i guess (Jerome)
	 */
	size = cs->dwords * sizeof(uint32_t);
	packets = drm_alloc(size, DRM_MEM_DRIVER);
	if (packets == NULL) {
		return -ENOMEM;
	}
	if (DRM_COPY_FROM_USER(packets, (void __user *)(unsigned long)cs->packets, size)) {
		r = -EFAULT;
		goto out;
	}

	chunk_fake[0].chunk_id = RADEON_CHUNK_ID_OLD;
	chunk_fake[0].length_dw = cs->dwords;
	chunk_fake[0].kdata = packets;

	parser.dev = dev;
	parser.file_priv = fpriv;
	parser.num_chunks = 1;
	parser.chunks = chunk_fake;
	parser.ib_index = 0;
	parser.reloc_index = -1;

	/* get ib */
	r = dev_priv->cs.ib_get(&parser, &card_offset);
	if (r) {
		goto out;
	}

	/* now parse command stream */
	r = dev_priv->cs.parse(&parser);
	if (r) {
		goto out;
	}

	BEGIN_RING(4);
	OUT_RING(CP_PACKET0(RADEON_CP_IB_BASE, 1));
	OUT_RING(card_offset);
	OUT_RING(cs->dwords);
	OUT_RING(CP_PACKET2());
	ADVANCE_RING();

	/* emit cs id sequence */
	dev_priv->cs.id_emit(dev, &cs_id);
	COMMIT_RING();

	cs->cs_id = cs_id;
out:
	dev_priv->cs.ib_free(&parser);
	drm_free(packets, size, DRM_MEM_DRIVER);
	return r;
}

/* for non-mm */
static int radeon_nomm_relocate(struct drm_radeon_cs_parser *parser, uint32_t *reloc, uint32_t *offset)
{
	*offset = reloc[1];
	return 0;
}
#define RELOC_SIZE 2
#define RELOC_SIZE_NEW 0
#define RADEON_2D_OFFSET_MASK 0x3fffff

static __inline__ int radeon_cs_relocate_packet0(struct drm_radeon_cs_parser *parser, uint32_t offset_dw)
{
	struct drm_device *dev = parser->dev;
	drm_radeon_private_t *dev_priv = dev->dev_private;
	uint32_t hdr, reg, val, packet3_hdr;
	uint32_t tmp, offset;
	struct drm_radeon_kernel_chunk *ib_chunk;
	int ret;

	ib_chunk = &parser->chunks[parser->ib_index];
//	if (parser->reloc_index == -1)
//		is_old = 1;

	hdr = ib_chunk->kdata[offset_dw];
	reg = (hdr & R300_CP_PACKET0_REG_MASK) << 2;
	val = ib_chunk->kdata[offset_dw + 1];
	packet3_hdr = ib_chunk->kdata[offset_dw + 2];

	/* this is too strict we may want to expand the length in the future and have
	 old kernels ignore it. */ 
	if (parser->reloc_index == -1) {
		if (packet3_hdr != (RADEON_CP_PACKET3 | RADEON_CP_NOP | (RELOC_SIZE << 16))) {
			DRM_ERROR("Packet 3 was %x should have been %x: reg is %x\n", packet3_hdr, RADEON_CP_PACKET3 | RADEON_CP_NOP | (RELOC_SIZE << 16), reg);
			return -EINVAL;
		}
	} else {
		if (packet3_hdr != (RADEON_CP_PACKET3 | RADEON_CP_NOP | (RELOC_SIZE_NEW << 16))) {
			DRM_ERROR("Packet 3 was %x should have been %x: reg is %x\n", packet3_hdr, RADEON_CP_PACKET3 | RADEON_CP_NOP | (RELOC_SIZE_NEW << 16), reg);
			return -EINVAL;

		}
	}
	
	switch(reg) {
	case RADEON_DST_PITCH_OFFSET:
	case RADEON_SRC_PITCH_OFFSET:
		/* pass in the start of the reloc */
		ret = dev_priv->cs.relocate(parser, ib_chunk->kdata + offset_dw + 2, &offset);
		if (ret)
			return ret;
		tmp = (val & RADEON_2D_OFFSET_MASK) << 10;
		val &= ~RADEON_2D_OFFSET_MASK;
		offset += tmp;
		offset >>= 10;
		val |= offset;
		break;
	case RADEON_RB3D_COLOROFFSET:
	case R300_RB3D_COLOROFFSET0:
	case R300_ZB_DEPTHOFFSET:
	case R300_TX_OFFSET_0:
	case R300_TX_OFFSET_0+4:
	case R200_PP_TXOFFSET_0:
	case R200_PP_TXOFFSET_1:
	case RADEON_PP_TXOFFSET_0:
	case RADEON_PP_TXOFFSET_1:
	        ret = dev_priv->cs.relocate(parser. ib_chunk->kdata + offset_dw + 2, &offset);
		if (ret)
			return ret;

		offset &= 0xffffffe0;
		val += offset;
		break;
	default:
		break;
	}

	ib_chunk->kdata[offset_dw + 1] = val;
	return 0;
}

static int radeon_cs_relocate_packet3(struct drm_radeon_cs_parser *parser,
				      uint32_t offset_dw)
{
	drm_radeon_private_t *dev_priv = parser->dev->dev_private;
	uint32_t hdr, num_dw, reg;
	uint32_t offset, val, tmp;
	int ret;
	struct drm_radeon_kernel_chunk *ib_chunk;

	ib_chunk = &parser->chunks[parser->ib_index];
//	if (parser->reloc_index == -1)
//		is_old = 1;

	hdr = ib_chunk->kdata[offset_dw];
	num_dw = (hdr & RADEON_CP_PACKET_COUNT_MASK) >> 16;
	reg = hdr & 0xff00;

	switch(reg) {
	case RADEON_CNTL_HOSTDATA_BLT:
	{
		val = ib_chunk->kdata[offset_dw + 2];
		ret = dev_priv->cs.relocate(parser, ib_chunk->kdata + offset_dw + num_dw + 2, &offset);
		if (ret)
			return ret;

		tmp = (val & RADEON_2D_OFFSET_MASK) << 10;
		val &= ~RADEON_2D_OFFSET_MASK;
		offset += tmp;
		offset >>= 10;
		val |= offset;

		ib_chunk->kdata[offset_dw + 2] = val;
	}
	default:
		return -EINVAL;
	}
	return 0;
}

int radeon_cs_packet0(struct drm_radeon_cs_parser *parser, uint32_t offset_dw)
{
	uint32_t hdr, num_dw, reg;
	int count_dw = 1;
	int ret;

	hdr = parser->chunks[parser->ib_index].kdata[offset_dw];
	num_dw = ((hdr & RADEON_CP_PACKET_COUNT_MASK) >> 16) + 2;
	reg = (hdr & R300_CP_PACKET0_REG_MASK) << 2;

	while (count_dw < num_dw) {
		/* need to have something like the r300 validation here - 
		   list of allowed registers */
		int flags;

		ret = r300_check_range(reg, 1);
		switch(ret) {
		case -1:
			DRM_ERROR("Illegal register %x\n", reg);
			break;
		case 0:
			break;
		case 1:
			flags = r300_get_reg_flags(reg);
			if (flags == MARK_CHECK_OFFSET) {
				if (num_dw > 2) {
					DRM_ERROR("Cannot relocate inside type stream of reg0 packets\n");
					return -EINVAL;
				}

				ret = radeon_cs_relocate_packet0(parser, offset_dw);
				if (ret)
					return ret;
				DRM_DEBUG("need to relocate %x %d\n", reg, flags);
				/* okay it should be followed by a NOP */
			} else if (flags == MARK_CHECK_SCISSOR) {
				DRM_DEBUG("need to validate scissor %x %d\n", reg, flags);
			} else {
				DRM_DEBUG("illegal register %x %d\n", reg, flags);
				return -EINVAL;
			}
			break;
		}
		count_dw++;
		reg += 4;
	}
	return 0;
}

int radeon_cs_parse(struct drm_radeon_cs_parser *parser)
{
	volatile int rb;
	struct drm_radeon_kernel_chunk *ib_chunk;
	/* scan the packet for various things */
	int count_dw = 0, size_dw;
	int ret = 0;

	ib_chunk = &parser->chunks[parser->ib_index];
	size_dw = ib_chunk->length_dw;

	while (count_dw < size_dw && ret == 0) {
		int hdr = ib_chunk->kdata[count_dw];
		int num_dw = (hdr & RADEON_CP_PACKET_COUNT_MASK) >> 16;
		int reg;

		switch (hdr & RADEON_CP_PACKET_MASK) {
		case RADEON_CP_PACKET0:
			ret = radeon_cs_packet0(parser, count_dw);
			break;
		case RADEON_CP_PACKET1:
		case RADEON_CP_PACKET2:
			reg = hdr & RADEON_CP_PACKET0_REG_MASK;
			DRM_DEBUG("Packet 1/2: %d  %x\n", num_dw, reg);
			break;

		case RADEON_CP_PACKET3:
			reg = hdr & 0xff00;
			
			switch(reg) {
			case RADEON_CNTL_HOSTDATA_BLT:
				radeon_cs_relocate_packet3(parser, count_dw);
				break;

			case RADEON_CNTL_BITBLT_MULTI:
			case RADEON_3D_LOAD_VBPNTR:	/* load vertex array pointers */
			case RADEON_CP_INDX_BUFFER:
				DRM_ERROR("need relocate packet 3 for %x\n", reg);
				break;

			case RADEON_3D_DRAW_IMMD:	/* triggers drawing using in-packet vertex data */
			case RADEON_CP_3D_DRAW_IMMD_2:	/* triggers drawing using in-packet vertex data */
			case RADEON_CP_3D_DRAW_VBUF_2:	/* triggers drawing of vertex buffers setup elsewhere */